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.mzph.cn/news/588727.shtml

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

相关文章

idea 出现Cannot resolve symbol ‘springframework‘解决方法

Maven手动重新加载 1&#xff09;File–>Invalidate Caches / Restart… 清理缓存&#xff0c;重启idea客户端 2&#xff09;File–>Maven–>Reload project重新从maven中加载工程依赖的组件

51单片机项目(24)——基于51单片机的温控风扇protues仿真

1.功能设计 使用传感器测量温度&#xff0c;并将温度显示在LCD1602上。如果温度超过阈值&#xff0c;那么就打开风扇&#xff0c;否则风扇不打开。&#xff08;仿真的时候&#xff0c;用直流电机模拟风扇&#xff09;。 仿真截图如下&#xff1a; 此时温度是27度&#xff0c;我…

FA组件详解

1、了解FA核心组件以及功能 &#xff08;1&#xff09;TC&#xff08;Thin Client&#xff1a;瘦终端&#xff09;&#xff1a;就是类似于机顶盒的一个小盒子&#xff0c;里面有CPU、内存、USB、MIC、HDMI等接口&#xff0c;可以理解为小型电脑&#xff0c;但是它里面是没有操作…

CSS 文字溢出:多行溢出、一行溢出

CSS 文字溢出&#xff1a;多行溢出、一行溢出 案例请点击查看文章详情。 代码如下&#xff1a; 多行溢出设置&#xff1a; .line-clamp-2{/* height: 52px;line-height: 25px; */overflow: hidden;word-break: break-all;text-overflow: ellipsis;display: -webkit-box;-webkit…

Leetcode的AC指南 —— 字符串:344. 反转字符串

摘要&#xff1a; Leetcode的AC指南 —— 字符串&#xff1a;344. 反转字符串。题目介绍&#xff1a;编写一个函数&#xff0c;其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。不要给另外的数组分配额外的空间&#xff0c;你必须原地修改输入数组、使用 …

【C#与Redis】--实践案例--案例 1:使用 Redis 实现缓存

在使用 Redis 实现缓存的案例中&#xff0c;我们可以使用 StackExchange.Redis 库&#xff0c;这是一个为 .NET 提供的 Redis 客户端库。以下是一个简单的使用 Redis 缓存的 C# 示例&#xff1a; 首先&#xff0c;你需要安装 StackExchange.Redis 库。可以通过 NuGet 包管理器…

python使用openpyxl操作excel

文章目录 前提读取已有excel创建一个excel工作簿对象创建excel工作簿中的工作表获取工作表第一种&#xff1a;.active 方法第二种&#xff1a;通过工作表名获取指定工作表​​​​​​第三种&#xff1a;.get_sheet_name() 修改工作表的名称数据操作写入数据按单元格写入通过指…

ArkTS语言基础入门学习-鸿蒙开发

文章目录 前言ArkTS简介统一的开发体验ArkTS语言优势ArkTS语言实战演示ArkTS语言的性能与跨平台适配ArkTS和TypeScript区别总结前言 本篇文章将深入介绍鸿蒙开发的主力语言——ArkTS语言,并通过比较传统网页开发模式和ArkTS开发模式,揭示ArkTS语言的独特之处以及其带来的开发…

2021-07-03 51单片机1.高低4位交替8次,2.从0到255,3.1+2+3+4...

缘由求老哥帮做一下单片机题。_嵌入式-CSDN问答 #include "REG52.h" bit k1; void main() {unsigned char Xd0,ss15,cs0;unsigned int ys64000;while(1){P1ss;if(ys0&&cs<8){k~k;cs;ss(k?15:240);ys64000;}} } #include "REG52.h" bit k1; v…

OpenGL ES案例学习-画板

#import "PaintView.h" #import <QuartzCore/QuartzCore.h> #import <GLKit/GLKit.h> #import <OpenGLES/EAGLDrawable.h> #import "debug.h" #import "shaderUtil.h" #import "fileUtil.h" //画笔透明度 #define k…

mobilevit v3 学习笔记

目录 原理讲解:不是特别全,可供参考: torch实现代码: 有预训练:

深信服AF防火墙配置SSL VPN

防火墙版本&#xff1a;8.0.85 需提前确认防火墙是是否有SSL VPN的授权&#xff0c;确认授权用户数量 1、确认内外网接口划分 2、网络→SSL VPN&#xff0c;选择内外网接口地址 3、SSL VPN→用户管理→新增一个SSL VPN的用户 4、新增L3VPN资源&#xff0c;类型选择Other&…

【基础】【Python网络爬虫】【1.认识爬虫】什么是爬虫,爬虫分类,爬虫可以做什么

Python网络爬虫基础 认识爬虫1.什么是爬虫2.爬虫可以做什么3.为什么用 Ptyhon 爬虫4.爬虫的分类通用爬虫聚焦爬虫功能爬虫增量式爬虫分布式爬虫 5.爬虫的矛与盾&#xff08;重点&#xff09;6.盗亦有道的君子协议robots7.爬虫合法性探究 认识爬虫 1.什么是爬虫 网络爬虫&…

第5课 使用openCV捕获摄像头并实现预览功能

这节课我们开始利用ffmpeg和opencv来实现一个rtmp推流端。推流端的最基本功能其实就两个:预览画面并将画面和声音合并后推送到rtmp服务器。 一、FFmpeg API 推流的一般过程 1.引入ffmpeg库&#xff1a;在代码中引入ffmpeg库&#xff0c;以便使用其提供的功能。 2.捕获摄像头…

MongoDB的基本使用

MongoDB的引出 使用Redis技术可以有效的提高数据访问速度&#xff0c;但是由于Redis的数据格式单一性&#xff0c;无法操作结构化数据&#xff0c;当操作对象型的数据时&#xff0c;Redis就显得捉襟见肘。在保障访问速度的情况下&#xff0c;如果想操作结构化数据&#xff0c;…

Spark中的数据加载与保存

Apache Spark是一个强大的分布式计算框架&#xff0c;用于处理大规模数据。在Spark中&#xff0c;数据加载与保存是数据处理流程的关键步骤之一。本文将深入探讨Spark中数据加载与保存的基本概念和常见操作&#xff0c;包括加载不同数据源、保存数据到不同格式以及性能优化等方…

20231231_小米音箱接入GPT

参考资料&#xff1a; GitHub - yihong0618/xiaogpt: Play ChatGPT and other LLM with Xiaomi AI Speaker *.设置运行脚本权限 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned *.配置小米音箱 ()pip install miservice_fork -i https://pypi.tuna.tsinghua.edu.cn/sim…

算法逆袭之路(1)

11.29 开始跟进算法题进度! 每天刷4题左右 ,一周之内一定要是统一类型 而且一定稍作总结, 了解他们的内在思路究竟是怎样的!! 12.24 一定要每天早中晚都要复习一下 早中午每段一两道, 而且一定要是同一个类型, 不然刷起来都没有意义 12.26/27&#xff1a; 斐波那契数 爬…

B3610 [图论与代数结构 801] 无向图的块 题解

B3610 [图论与代数结构 801] 无向图的块 题解 2023 2023 2023&#xff0c;再见。 2024 2024 2024&#xff0c;你好&#xff01; 解法 其实就是统计点双连通分量的个数。需要注意的是&#xff0c;孤立点在这里不被看作块。本文使用 tarjan 算法来解决这道题。 概念明晰 时间…

机器学习:贝叶斯估计在新闻分类任务中的应用

文章摘要 随着互联网的普及和发展&#xff0c;大量的新闻信息涌入我们的生活。然而&#xff0c;这些新闻信息的质量参差不齐&#xff0c;有些甚至包含虚假或误导性的内容。因此&#xff0c;对新闻进行有效的分类和筛选&#xff0c;以便用户能够快速获取真实、有价值的信息&…