r语言导入ggplot2_R语言 -- ggplot2 学习

本文介绍了使用R语言ggplot2库进行数据可视化的基本步骤和技巧,包括绘制箱线图、调整图形样式、设置标题与坐标轴属性、自定义图例等,展示了从基础图表到精美图表的转化过程。
摘要由CSDN通过智能技术生成

菜鸡又来学画图了,菜鸡总共整理了大概一天半

这些当然只是冰山一角,以后遇到其他问题会继续补充,奥利GAY!

原始图:

美化后(自认为):

library("ggplot2")

# 随便挑个数据集

data("iris")

str(iris)

# 基础版

aa = ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot()

# 手动再加个类型

iris$class = rep(c("A","B","C"), 50)

ab = ggplot(iris, aes(x = Species, y = Sepal.Length, color = class))

ab + geom_boxplot()

# 设置box的宽度和去除离群点

ac = ab + stat_boxplot(geom = "errorbar", width=0.6)

ac2 = ac + geom_boxplot(width = 0.6, outlier.shape=NA)

# 设置y轴范围,去除坐标轴边缘多余的线

ad = ac2 + scale_y_continuous(expand = c(0, 0),

limits = c(4, 8),

breaks = seq(4, 8, 0.5))

ae = ad + theme_bw()

# 设置经典的主题;添加主标题和坐标轴标题

af = ae + labs(x = "This is x-title",

y = "This is y-title",

title = "This is title",

subtitle = "This is subtitle")

af2 = af + theme(plot.title = element_text(size = 25,

color = "#E90090",

face = "bold",

hjust = 0.5),

plot.subtitle = element_text(size = 15,

color = "#00B6E9",

face = "italic",

hjust = 0.5))

# 设置坐标轴标题颜色、大小、加粗、线体

ag = af2 + theme(axis.title.x = element_text(color = "black",

size = 15,

face = "bold",

vjust = -5),

axis.title.y = element_text(color = "black",

size = 15,

face = "bold"))

# 调整画布大小

ag2 = ag + theme(plot.margin=unit(rep(0.2, 4), "inch"))

# 设置坐标轴标签倾斜方向,大小、颜色

ah

hjust = 1,

colour = "#3D3D3D",

size = 15),

axis.text.y = element_text(angle = 30,

hjust = 1,

colour = "#3D3D3D",

size = 15))

# # 清除网格

ai = ah + theme(panel.grid.major = element_blank(),

panel.grid.minor = element_blank(),

panel.background = element_blank(),

# axis.line = element_line(colour = "black"),

panel.border = element_rect(colour = "black", fill=NA))

# 隐藏图例

ai + theme(legend.position = "none")

# 修改图例标题内容、图例标签名称

# 自定义fill的颜色

ak = ai + scale_color_manual(values=c("#E90B91", "#E88C00", "#231FEC"),

name="My legend",

breaks=c("C", "B", "A"),

labels=c("Key_C", "Key_B", "Key_A"))

# 修改图例标题样式

al = ak + theme(legend.title=element_text(size=15,

colour = "#8100A9",

angle = 45,

hjust = 0.4,

vjust = 0.4))

# 隐藏图例标题

al2 = al + theme(legend.title=element_blank())

# 修改图例标签样式

am = al2 + theme(legend.text = element_text(colour = "#F7772E",

angle = 45,

size=13,

# hjust = -1.5,

vjust = 0.4,

face="bold"))

# 设置legend边框

am + theme(legend.background =

element_rect(

fill = NA,

size = 0.5,

linetype = "dotted",

color = "black"

)

)

# 去除key背景

am2 = am + theme(legend.key =

element_rect(

fill = NA,

size = 30

)

)

# 修改图例key大小

an = am2 + theme(legend.key.width=unit(0.4,"inches"),

legend.key.height=unit(0.4,"inches"))

# 修改图例方向、位置

ao = an + theme(legend.direction = "horizontal",

legend.position = c(0.27, 0.8))

# 添加抖动的散点图,但是如何让散点图分布在对应箱线图内呢?

ap = ao + geom_jitter(

aes(color = class),

position = position_jitter(w = 0.1, h = 0.05),

size = 1,

alpha = 0.4

)

# 调整X轴标题位置

aq = ao + theme(axis.title.x = element_text(vjust = -2))

添加主次ticks

如何解决文字溢出问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值