首先是构建数据集
df <- data.frame(x = 1:10, y = (1:10)^2)
最基本的散点图
ggplot(df, aes(x = x, y = y)) +
geom_point()

去掉灰色背景
参考
Remove elements from ggplot
ggplot(df, aes(x = x, y = y)) +
geom_point()+
theme(panel.background = element_blank())

添加坐标轴的竖线
https://ggplot2.tidyverse.org/reference/theme.html
ggplot(df, aes(x = x, y = y)) +
geom_point()+
theme(panel.background = element_blank(),
axis.line.y = element_line(color="red",size=5))

在右边再加一个坐标轴
参考 https://ggplot2.tidyverse.org/reference/sec_axis.html
这个链接还有如何操作第二个坐标轴的一些其他例子
ggplot(df, aes(x = x, y = y)) +
geom_point()+
theme(panel.background = element_blank(),
axis.line.y = element_line(color="red",size=5))+
scale_y_continuous(sec.axis = dup_axis())

去掉坐标轴上的文字(text)
ggplot(df, aes(x = x, y = y)) +
geom_point()+
theme(panel.background = element_blank(),
axis.line.y = element_line(color="red",size=5),
axis.text.x = element_blank())+
scale_y_continuous(sec.axis = dup_axis())

去掉坐标轴上的小短线(ticks)
ggplot(df, aes(x = x, y = y)) +
geom_point()+
theme(panel.background = element_blank(),
axis.line.y = element_line(color="red",size=5),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())+
scale_y_continuous(sec.axis = dup_axis())

去掉坐标轴标题 (title)
ggplot(df, aes(x = x, y = y)) +
geom_point()+
theme(panel.background = element_blank(),
axis.line.y = element_line(color="red",size=5),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank())+
scale_y_continuous(sec.axis = dup_axis())

欢迎大家关注我的公众号
小明的数据分析笔记本
网友评论