R_handbook_作图专题

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_yeardata_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.hqwc.cn/news/312560.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

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…

「实验记录」CS144 Lab1 StreamReassembler

目录 一、Motivation二、SolutionsS1 - StreamReassembler的对外接口S2 - push_substring序列写入ByteStream 三、Result四、My Code五、Reference 一、Motivation 我们都知道 TCP 是基于字节流的传输方式&#xff0c;即 Receiver 收到的数据应该和 Sender 发送的数据是一样的…

.NET进阶篇06-async异步、thread多线程2

知识须要不断积累、总结和沉淀&#xff0c;思考和写做是成长的催化剂web 内容目录 1、线程Thread 一、生命周期 二、后台线程 三、静态方法 1.线程本地存储 2.内存栅栏 四、返回值 2、线程池ThreadPool 一、工做队列 二、工做线程和IO线程 三、和Thread区别 四、定时器 1、线…

概率的 50 个具有挑战性的问题 [8/50]:完美的桥牌

一、说明 我最近对与概率有关的问题产生了兴趣。我偶然读到了弗雷德里克莫斯特勒&#xff08;Frederick Mosteller&#xff09;的《概率论中的五十个具有挑战性的问题与解决方案》&#xff09;一书。我认为创建一个系列来讨论这些可能作为面试问题出现的迷人问题会很有趣。每篇…

【Redis-03】Redis数据结构与对象原理 -下篇

承接上篇【Redis-02】Redis数据结构与对象原理 -上篇 8. type-字符串string 8.1 字符串的三种encoding编码&#xff08;int embstr raw&#xff09; 如果保存的是整型&#xff0c;并且可以用long类型标识&#xff08;-9223372036854775808到9223372036854775807&#xff09…

怎么挑选猫粮?挑选主食冻干猫粮的步骤

各位铲屎官都知道猫天性是食肉动物&#xff0c;无肉不欢的。而冻干猫粮对于猫咪来说是最好还原猫咪食肉天性的食物&#xff0c;不仅可以当成猫咪的主食&#xff0c;也可以用来给猫咪当成零食&#xff0c;帮助猫咪补充营养。冻干猫粮是经过真空冷冻干燥处理的鲜肉&#xff0c;能…

菜鸟网络Java实习一面面经

自我介绍&#xff0c;做过的项目 巴拉巴拉 你项目中用到redis&#xff0c;可以介绍一下为什么使用它吗&#xff1f; 基于内存操作&#xff0c;内存读写速度快。 支持多种数据类型&#xff0c;包括String、Hash、List、Set、ZSet等。 支持持久化。Redis支持RDB和AOF两种持久…

探索 Vue 异步组件的世界:解锁高效开发的秘密(上)

&#x1f90d; 前端开发工程师&#xff08;主业&#xff09;、技术博主&#xff08;副业&#xff09;、已过CET6 &#x1f368; 阿珊和她的猫_CSDN个人主页 &#x1f560; 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 &#x1f35a; 蓝桥云课签约作者、已在蓝桥云…

Vue-Setup

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

SpringCloud 和 Linux 八股文第三期五问五答

SpringCloud 和 Linux 八股文第三期五问五答 作者&#xff1a;程序员小白条&#xff0c;个人博客 相信看了本文后&#xff0c;对你的面试是有一定帮助的&#xff01; ⭐点赞⭐收藏⭐不迷路&#xff01;⭐ 1&#xff09;Linux常用命令 2&#xff09;如何查看测试项目的日志 一…

docker搭建minio集群,集群分享文件URL踩坑问题

一、环境准备 3台机器&#xff0c;Ip地址依次为IP1,IP2,IP3二、设置服务器时间同步 Minio集群需要各个节点的时间保持同步&#xff0c;使用NTP作为时间同步服务&#xff0c;这里以Minio-1&#xff08;IP1&#xff09;为上游服务器&#xff0c;其它2个节点为下游服务器&#x…

【YOLO系列】yolo V1 ,V3,V5,V8 解释

文章目录 yolo V1 模型结构图通道数 的 物理意义是什么&#xff1f;输出 7730 怎么理解&#xff1f;YOLO v1 损失函数LOSS yolo V3yolo V5yolo V8 视频来源&#xff1a;https://www.bilibili.com/video/BV13K411t7Zs/ AI视频小助理 一、YOLO系列的目标检测算法&#xff0c;其中…