R_handbook_作图专题

news2025/2/25 7:52:12

ggplot基本作图





1 条形图

library(ggplot2)
ggplot(biopics) + 
  geom_histogram(aes(x = year_release),binwidth=1,fill="gray")

2 堆砌柱状图

ggplot(biopics, aes(x=year_release)) +
  geom_bar(aes(fill=subject_sex))

3 堆砌比例柱状图

ggplot(biopics, aes(x=year_release)) +
  geom_bar(aes(fill=subject_sex),position = 'fill')

4 马赛克图

library(vcd)  
bio_ques_d <- biopics[,c(11,13)]
bio_ques_d$subject_race <- ifelse(is.na(bio_ques_d$subject_race ), "missing",
                                 ifelse(bio_ques_d$subject_race == "White","White", "nonwhite"))
biq_ques_d_table <- table(bio_ques_d$subject_race,bio_ques_d$subject_sex)
mosaicplot(biq_ques_d_table) 

5 双散点图

process_var <- c('v32', 'v33', 'v34', 'v35', 'v36', 'v37')
for (i in c(1:6)){
  var_clean <- paste(process_var[i],'clean',sep = '_')
  data[,var_clean] <- ifelse(data[,process_var[i]] == 'trust completely',1,
                             ifelse(data[,process_var[i]] == 'trust somewhat',2,
                                    ifelse(data[,process_var[i]] == 'do not trust very much',3,
                                           ifelse(data[,process_var[i]] == 'do not trust at all',4,NA))))
}
data$intp.trust <- rowSums(data[,c(438:443)],na.rm = TRUE)
data$intp.trust <- data$intp.trust/6
ggplot(data[data$country == 'Iceland',], aes(x=confidence, y=intp.trust, colour=v225)) + geom_point()

6 双密度图

ggplot(data=start_s_country_data) +
  geom_density(aes(x=residual,color=as.factor(v225),))

## 自定义图例的情况
ggplot(data=data) +
  geom_density(aes(x=LW, color = "LW")) + 
  geom_density(aes(x=LP, color = "LP")) + 
  labs(title="") + 
  xlab("Value") + 
  theme(legend.title=element_blank(),
        legend.position = c(0.9, 0.9))

ggplot(data ) +
  geom_point(aes(x = No.education, y=Median.year.of.schooling)) + 
  geom_smooth(aes(x = No.education, y=Median.year.of.schooling), method = 'lm') + 
  theme_classic() 

7 双折线图与多图展示

library(dplyr)
library(devtools)
library(cowplot)

plot_grid(plot1,plot3,plot5,plot2,plot4,plot6,ncol=3,nrow=2)
bio_ques_f <- biopics[,c(4,11,13)]
bio_ques_f$subject_race <- ifelse(is.na(bio_ques_f$subject_race ), "missing",
                                 ifelse(bio_ques_f$subject_race == "White","White", "nonwhite"))

planes <- group_by(bio_ques_f, year_release, subject_race, subject_sex)
bio_ques_f_summary <- summarise(planes, count = n())
planes <- group_by(bio_ques_f,year_release)
bio_ques_f_year<- summarise(planes,count_year = n())

bio_ques_f_summary <- left_join(bio_ques_f_summary,bio_ques_f_year,c("year_release" = "year_release"))
bio_ques_f_summary$prop <- bio_ques_f_summary$count / bio_ques_f_summary$count_year

data_missing_female <- subset(bio_ques_f_summary,with(bio_ques_f_summary,(subject_race == 'missing') & (subject_sex == 'Female')))
data_missing_male <- subset(bio_ques_f_summary,with(bio_ques_f_summary,(subject_race == 'missing') & (subject_sex == 'Male')))
data_nonwhite_female <- subset(bio_ques_f_summary,with(bio_ques_f_summary,(subject_race == 'nonwhite') & (subject_sex == 'Female')))
data_nonwhite_male <- subset(bio_ques_f_summary,with(bio_ques_f_summary,(subject_race == 'nonwhite') & (subject_sex == 'Male')))
data_white_female <- subset(bio_ques_f_summary,with(bio_ques_f_summary,(subject_race == 'White') & (subject_sex == 'Female')))
data_white_male <- subset(bio_ques_f_summary,with(bio_ques_f_summary,(subject_race == 'White') & (subject_sex == 'Male')))


plot1 <- ggplot(data_missing_female)+
           geom_line(aes(x=year_release,y=count),color="red") + 
           geom_line(aes(x=year_release,y=prop),color="blue") +
           labs(title="missing and female")
plot2 <- ggplot(data_missing_male)+
           geom_line(aes(x=year_release,y=count),color="red") + 
           geom_line(aes(x=year_release,y=prop),color="blue") +
           labs(title="missing and male")
plot3 <- ggplot(data_nonwhite_female)+
           geom_line(aes(x=year_release,y=count),color="red") + 
           geom_line(aes(x=year_release,y=prop),color="blue") +
           labs(title="nonwhite and female")
plot4 <- ggplot(data_nonwhite_male)+
           geom_line(aes(x=year_release,y=count),color="red") + 
           geom_line(aes(x=year_release,y=prop),color="blue") +
           labs(title="nonwhite and male")
plot5 <- ggplot(data_white_female)+
           geom_line(aes(x=year_release,y=count),color="red") + 
           geom_line(aes(x=year_release,y=prop),color="blue") +
           labs(title="white and female")
plot6 <- ggplot(data_white_male)+
           geom_line(aes(x=year_release,y=count),color="red") + 
           geom_line(aes(x=year_release,y=prop),color="blue") +
           labs(title="white and male")

plot_grid(plot1,plot3,plot5,plot2,plot4,plot6,ncol=3,nrow=2)

ggplot作图美化

1 标题居中

ggplot(data_selected, aes(x=AREA.NAME)) +
  geom_bar(aes(fill=year)) + 
  labs(title = 'The bar plot of AREA.NAME') +
  theme_classic() + 
  theme(plot.title = element_text(hjust = 0.5))

2 X轴标签旋转

ggplot(data_selected, aes(x=AREA.NAME)) +
  geom_bar(aes(fill=year)) + 
  labs(title = 'The bar plot of AREA.NAME') +
  theme_classic() + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  theme(axis.text.x=element_text(face="bold",size=8,angle=270,color="black"))

3 变更label名

ggplot(data=data) + 
  geom_line(aes(x=index,y=data,group=line,color=result)) + 
  theme_classic() + 
  scale_colour_manual(values=c("red", "blue"), labels=c("lose", "win")) 

ggforce

ggforce能对绘制的图增加聚类图层,包括圆形、椭圆形、方形能多种。

North_latitude <- c(47.5, 52.3, 54.8, 48.4, 54.2,
                    54.8, 54.4, 48.8, 50.5, 52.7,
                    46.5, 46.9, 45.1, 45.9, 50.7,
                    48.5, 48.3, 48.1, 48.8, 49.4)
Elevation <- c(2, 1, 1, 2, 1,
               1, 1, 2, 2, 1,
               2, 2, 2, 2, 1,
               2, 2, 1, 1, 1)
Temperature <- c(39.27, 39.00, 38.35, 37.58, 39.38,
                 39.05, 39.65, 38.66, 37.97, 40.10,
                 37.05, 37.19, 36.92, 36.70, 38.01,
                 37.26, 36.97, 36.95, 37.68, 37.55)
data <- data.frame(North_latitude = North_latitude,
                   Elevation = Elevation,
                   Temperature = Temperature)
data$Elevation <- as.factor(data$Elevation)
dim(data)

library(ggplot2)
library(ggforce)
ggplot(data=data,aes(x=North_latitude,y=Temperature,color=Elevation))+
geom_point()+
geom_mark_circle(aes(fill=Elevation),alpha=0.4)+
theme_classic() +
labs(title = 'The relationship between latitude and temperature') +
  theme(plot.title = element_text(hjust = 0.5))

地理位置图

library(ggplot2)
library(viridis)
library(cvTools)
library(dplyr)

data <- read.csv("Reef_Check_with_cortad_variables_with_annual_rate_of_SST_change.csv")

world_map <- map_data("world")
ggplot() + 
  geom_polygon(data =world_map, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
  geom_point(data =data, alpha = 0.2, aes(y=Latitude.Degrees, x= Longitude.Degrees , size=Average_bleaching, color=Average_bleaching))  + scale_colour_viridis() + theme_minimal()

igraph网络图

library(igraph)

webforum_graph <- webforum[webforum$Date > as.Date("2010-12-01"), ]
webforum_graph <- webforum_graph[webforum_graph$Date < as.Date("2010-12-31"), ]

# generate node dataframe
AuthorID <- unique(as.numeric(webforum_graph$AuthorID))
ThreadID <- unique(as.numeric(webforum_graph$ThreadID))
name <- c(AuthorID, ThreadID)
type <- c(rep("Author", length(AuthorID)) , rep("Thread", length(ThreadID)))
webforum_node <- data.frame(name = name, type = type)

# generate edge dataframe
webforum_graph <- webforum_graph[,c("AuthorID", "ThreadID")]

# generate graph dataframe
graph <- graph_from_data_frame(webforum_graph, directed = FALSE, vertices=webforum_node) 


set.seed(30208289)

plot(graph,  
     layout= layout.fruchterman.reingold,  
     vertex.size=10,   
     vertex.shape="circle",    
     vertex.color=ifelse(V(graph)$type == "Thread", "red", "blue"),
     vertex.label=NULL, 	 
     vertex.label.cex=0.7,    
     vertex.label.color='black',  
     vertex.label.dist=0,
     edge.arrow.size=0.2, 
     edge.width = 0.5, 
     edge.label=V(graph)$year, 
     edge.label.cex=0.5,
     edge.color="black") 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1347680.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

echarts常见的一些大屏示意图及配置项【好看】

双立体柱状图 示意图&#xff1a; 配置&#xff1a; initData() {let sideData [220, 182, 191, 234, 290, 330]let sideData1 [100, 110, 120, 134, 190, 230]let nameList [结算能力数, 结算金额]let yAxisData [(金额/亿元), (能力数/个)]let xData [1, 2, 3, 4, 5…

三角函数两角和差公式推导

一.几何推理 1.两角和公式 做一斜边为1的直角△ABC,任意旋转非 k Π , k N kΠ,kN kΠ,kN,补充如图,令 ∠ A B C ∠ α &#xff0c; ∠ C B F ∠ β ∠ABC∠α&#xff0c;∠CBF∠β ∠ABC∠α&#xff0c;∠CBF∠β ∴ ∠ D B F ∠ D B A ∠ α ∠ β 90 , ∠ D A …

vue3-13

token可以是后端api的访问依据&#xff0c;一般绝大多数时候&#xff0c;前端要访问后端的api,后端都要求前端请求需要携带一个有效的token,这个token用于用户的身份校验&#xff0c;通过了校验&#xff0c;后端才会向前端返回数据&#xff0c;进行相应的操作&#xff0c;如果没…

自动驾驶学习笔记(二十四)——车辆控制开发

#Apollo开发者# 学习课程的传送门如下&#xff0c;当您也准备学习自动驾驶时&#xff0c;可以和我一同前往&#xff1a; 《自动驾驶新人之旅》免费课程—> 传送门 《Apollo开放平台9.0专项技术公开课》免费报名—>传送门 文章目录 前言 控制算法 控制标定 控制协议…

【漏洞复现】企望制造ERP系统 RCE漏洞

漏洞描述 企望制造ERP系统是畅捷通公司开发的一款领先的生产管理系统&#xff0c;它以集成化管理为核心设计理念&#xff0c;通过模块化机制&#xff0c;帮助企业实现生产、采购、库存等方面的高效管理。该系统存在RCE远程命令执行漏洞&#xff0c;恶意攻击者可利用此漏洞进行…

【解决复杂链式任务打造全能助手】大模型思维链 CoT 应用:langchain 大模型 结合 做 AutoGPT

大模型思维链 CoT 应用&#xff1a;langchain 大模型 结合 做 AutoGPT&#xff0c;解决复杂链式任务打造全能助手 思维链 CoTlangchainlangchain 大模型结合打造 AutoGPT 思维链 CoT 最初的语言模型都是基于经验的&#xff0c;只能根据词汇之间的相关性输出答案&#xff0c;根…

Android 接入第三方数数科技平台

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、数数科技平台是什么&#xff1f;二、使用步骤1.集成SDK2. 初始化3. 发送事件和设置账号id4. 验证发送事件是否成功 小结 前言 一个成熟的App必然不可缺少对…

FingerprintService启动-Android13

FingerprintService启动-Android13 1、指纹服务启动1.1 rc启动Binder对接指纹厂商TA库1.2 FingerprintService启动1.2.1 SystemServer启动FingerprintService1.2.2 注册Binder服务fingerprint 2、获取底层信息2.1 AIDL 对接TA中获取2.2 指纹类型判断 android13-release 1、指纹…

有限差分场的数值计算:代数、求导、积分

文章目录 前言一、代数运算1.手动计算流程2.ubermag库函数验证 二、求导运算1.手动计算流程2.ubermag库函数验证3.标量场的梯度&#xff0c;矢量场的散度和旋度 三、积分运算1.手动计算流程2.ubermag库函数验证 总结 靡不有初&#xff0c;鲜克有终。——《诗经大雅荡》 前言 …

fastApi 项目部署

方式一&#xff0c;Uvicorn部署 Run a Server Manually - Uvicorn - FastAPI 1&#xff0c;linux服务器安装 python>3.8 2&#xff0c;安装 uvicorn : pip install "uvicorn[standard]" 3&#xff0c;上传项目到服务器 main.py from typing imp…

机场信息集成系统系列介绍(6):机场协同决策支持系统ACDM*续集

目录 1、A-CDM实施效果评估背景 2、评估核心指标项 &#xff08;1&#xff09;机位效率 &#xff08;2&#xff09;登机效率 &#xff08;3&#xff09;推出效率 &#xff08;4&#xff09;滑行效率 &#xff08;5&#xff09;协同效率 3、其他指标项 &#xff08;1&a…

使用 Hyper-V 创建虚拟机

使用 Hyper-V 创建虚拟机 官网教程修改存储目录Hyper-V管理器创建虚拟机启动虚拟机Win10安装教程Press any key to boot from CD or DVD...... 如何使用Windows自带的虚拟机工具来创建虚拟机&#xff0c; 快速创建虚拟机进行学习探讨&#xff0c;如果有环境问题可以立即创建一个…

Vue-Setup

一、setup概述 小小提示&#xff1a;vue3中可以写多个根标签。 Person.vue中内容 <template><div class"person"><h2>姓名&#xff1a;{{name}}</h2><h2>年龄&#xff1a;{{age}}</h2><!--定义了一个事件&#xff0c;点击这…

【Image】超硬核数学推导——WGAN的先“破”后“立”

GAN的实现 上一篇文章中我们说到了GAN的数学解释 min ⁡ G max ⁡ D V ( D , G ) E x ∼ p data ( x ) [ log ⁡ D ( x ) ] E z ∼ p z ( z ) [ log ⁡ ( 1 − D ( G ( z ) ) ) ] − log ⁡ 4 2 J S D ( p data ∥ p g ) ≥ − log ⁡ 4 , where [ p d a t a p g ] \mi…

node相关的args属性与<param>子标签的区别

launch文件内&#xff1a;node标签内的<param>标签示例&#xff1a; 可以看到launch文件内的<param>标签在命令行内会转化为--ros-args -p 这样格式的命令&#xff0c;说明<param>标签指定的是ros2内的参数。不能用于传递非ros2的传入参数 如果要传入非ros2…

【测试基础】构造测试数据之 MySQL 篇

构造测试数据之 MySQL 篇 作为一名测试工程师&#xff0c;我们经常会构造测试数据进行一些功能验证。为了暴露更多的问题&#xff0c;在测试数据的构造上&#xff0c;我们应该尽可能的构造不同类型字段的数据&#xff0c;且一张表的字段最好不低于 10 10 10 个。 对于 MySQL …

在高并发场景下,缓存“雪崩”了怎么办

1. 缓存雪崩的常见原因 缓存“雪崩”是指&#xff0c;因为部分缓存节点不可用&#xff0c;而导致整个缓存系统&#xff08;甚至是整个服务系统&#xff09;不可用。缓存“雪崩”主要分为以下两种情况&#xff1a; 因缓存不支持 rehash 而导致的缓存“雪崩”缓存支持 rehash 时…

基于Vite创建简单Vue3工程

首先安装node.js环境&#xff0c;没有node.js环境&#xff0c;便没有npm命令。 1、Vue3创建执行命令 D:\TABLE\test>npm create vuelatestVue.js - The Progressive JavaScript Framework√ 请输入项目名称&#xff1a; ... vue_test √ 是否使用 TypeScript 语法&#xff…

很想写一个框架,比如,spring

很想写一个框架&#xff0c;比如&#xff0c;spring。 原理很清楚&#xff0c;源码也很熟悉。 可惜力不从心&#xff0c;是不是可以找几个小弟一起做。

Stata18软件安装包下载及安装教程

Stata 18下载链接&#xff1a;https://docs.qq.com/doc/DUm5pRlFJaWV5aWtY 1.选中下载好的安装包&#xff0c;右键选择解压到“Stata18”文件夹 2.选中“SetupStata18.exe”&#xff0c;右键以管理员身份运行 3.点击“Next” 4.选择“I accept.....”,选择“Next” 5.点击“Nex…