就是这个图,以前都是用graphpad做,我觉得R语言一定可以实现,就试了一下。
0.思路
1.找到ggplot2做曲线图的函数--geom_smooth。
2.ggplot2默认x、y轴都有一定的扩展,不是从0开始,找参数。
3.线型和颜色、性状均按数据分组来映射即可。
4.想让刻度线朝里,这个需要ggplot2里的参数实现,现查
1.生成示例数据
简单一点两组就好,意思意思。
y = c(1,0.8,0.7,0.66,0.6,0.57,0.55,0.52,0.5)
d1 = data.frame(xa = seq(0,62,7),
y1 = c(1,0.8,0.7,0.66,0.6,0.57,0.55,0.52,0.5),
y2 = c(1,0.6,0.4,0.3,0.25,0.22,0.17,0.15,0.13))
d2 = tidyr::gather(d1,"y","qd",-xa)
2.作图
让刻度从0开始很好查,就是expand=c(0,0)
library(ggplot2)
p = ggplot(data = d2,aes(x = xa,y=qd,color = y))+
geom_smooth(se = F)+
geom_point(aes(shape = y),size = 2)+
theme_classic()+
scale_x_continuous(limits = c(0,60),expand = c(0,0))+
scale_y_continuous(limits = c(0,1),expand = c(0,0))
p
调整坐标轴刻度线朝里就可以了,找到了一个axis.ticks.length,可以直接设置,但紧接着就出现了刻度标签的重叠,调整的参数是axis.text,但是,
axis.text里的vjust/hjust(分别表示垂直方向移动和水平方向移动)对x轴刻度标签有效,对y轴标签却无用??
我一度有点蒙圈,这么普适的包,应该是不会出错的吧。
p +
theme(axis.ticks.length.x.bottom = unit(-0.25, "cm")) +
theme(axis.ticks.length.y.left = unit(-0.25, "cm")) +
theme(axis.text.x.bottom = element_text(vjust=-5))+
theme(axis.text.y.left = element_text(hjust=-5))+
theme(axis.title.x = element_text(vjust=-2))+
theme(axis.title.y = element_text(hjust=-2))+
theme(plot.margin = unit(c(1,1,1,1), "cm"))
继续搜索,找到了解决办法,但还是没明白为何x轴的vjust好用,y轴的hjust就不行。我想过可能是图边距的问题,
theme(plot.margin = unit(c(1,1,1,1), "cm"))
就是设置图边距用的,没用。所以问题出在哪里呢???
解决办法倒是找到了:
p +
theme(axis.ticks.length=unit(-0.25, "cm"),
axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")),
axis.text.y = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")) )
最后的调整代码出自:
https://stackoverflow.com/questions/26367296/how-do-i-make-my-axis-ticks-face-inwards-in-ggplot2
一顿好找!!!!