我有以下问题:我想用ggplot创建一个图,显示不同处理组的两个变量(贻贝中的微量弹性量化,表示为MP和Lipofuscinaccumulation,表示为Lip)与暴露时间的独立性之间的关系。
我的数据如下所示:
这是我的代码:
ggplot(Catrv_all,aes(Lip,MP,color=treatment))+
geom_smooth(method="lm", se=FALSE)+
geom_point(size = 2)+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5))+
labs(x = "Lipofuscin accumulation [% area]",
y = "Microplastic quantification [% area]",
title = "Lipofuscin accumulation vs. Microplastic quantification")
情节如下:
我认识到ggplot显然没有以正确的方式为曝光时间排序值,因为值不一致(例如,它不是以0小时的值开始)。
我的问题是:我怎样才能告诉ggplot在曝光时间方面按照正确的顺序重新排序MP和Lip的值?我应该创建第二个x轴吗?如果是的话,我怎么能在ggplot中做到这一点?
我在SO中看到了很多讨论,很难在ggplot中创建第二个x / y轴,但我不知道如何以另一种方式显示我的数据。
更新我的问题:我听取了sconfluentus的建议,并在以下文章中找到了Ben Bolker的一个非常有趣的答案: How can I plot with 2 different y-axes?
我改编了提供的代码:
## add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 6) + 0.1)
## split data set for treatment groups
MP_Ko<-Catrv_all$MP[1:8]
exp<-Catrv_all$expTime[1:8]
Lip_Ko<-Catrv_all$Lip[1:8]
## Plot first set of data and draw its axis
plot(exp, MP_Ko, pch=16, axes=FALSE, xlab="", ylab="",
type="b",col="black", main="Microplastic quantification vs. Lipofuscin accumulation in Controls")
axis(2,col="black",las=1) ## las=1 makes horizontal labels
mtext("Microplastic quantification [% area]",side=2,line=2.5)
box()
## Allow a second plot on the same graph
par(new=TRUE)
## Plot the second plot and put axis scale on right
plot(exp,Lip_Ko, pch=15, xlab="", ylab="",
axes=FALSE, type="b", col="red")
## a little farther out (line=4) to make room for labels
mtext("Lipofuscin accumulation [% area]",side=4,col="red",line=4)
axis(4, col="red",col.axis="red",las=1)
## Draw the time axis
axis(1,pretty(range(Catrv_all$expTime, 672)))
mtext("Time (Hours)",side=1,col="black",line=2.5)
## Add Legend
legend("topright",legend=c("Microplastic quantification","Lipofuscin accumulation"),
text.col=c("black","red"),pch=c(16,15),col=c("black","red"))
...并得到以下情节: enter image description here
耗时,但这种方法非常有用。
答案 0 :(得分:-1)
谢谢大家的建议!我现在使用dput(Catrv_all)
,这是我的数据的输出:
structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("Co", "CoP", "HDPE"), class = "factor"),
expTime = c(0L, 3L, 6L, 24L, 96L, 168L, 336L, 672L, 0L, 3L,
6L, 24L, 96L, 168L, 336L, 672L, 0L, 3L, 6L, 24L, 96L, 168L,
336L, 672L), MP = c(0.056481655, 0.098508038, 0.097108112,
0.056848278, 0.082198187, 0.052261369, 0.022911461, 0.023901656,
0.056481655, 0.124866733, 0.125732967, 0.07986102, 0.071233133,
0.128376543, 0.331948, 0.121689155, 0.056481655, 0.186735799,
0.137477095, 0.41251914, 0.093364945, 0.085760245, 0.249371764,
0.187693319), Lip = c(9.848221569, 11.62875399, 9.530378924,
12.67745734, 14.14610784, 11.44140636, 11.55310567, 12.37321851,
9.848221569, 8.889567938, 12.5142123, 13.79770638, 11.26698845,
14.67064904, 14.56027915, 15.24772977, 9.848221569, 12.22424265,
13.05104725, 12.96830215, 12.10175574, 14.66505958, 13.67550035,
11.65168387), Cat = c(6.681571728, 7.321681629, 4.939885929,
7.73812502, 6.85066487, 9.317238053, 8.309505248, 9.33338377,
6.681571728, 7.517468479, 7.151607966, 9.074518192, 6.350614893,
9.749092742, 9.335634354, 11.43658695, 6.681571728, 6.164473371,
9.416062149, 9.19813927, 8.041328941, 8.736550013, 9.788258534,
10.55471537), CI = c(120.5252336, 110.1709456, 112.9077575,
110.9032308, 101.0274926, 101.1970679, 107.1464111, 97.42950278,
120.5252336, 101.7284063, 132.6162567, 108.7251954, 107.2199383,
102.9096767, 100.9637646, 101.6655302, 120.5252336, 102.1888777,
111.9139996, 113.7840225, 104.4767637, 103.1984161, 96.67797683,
95.59369834)), .Names = c("treatment", "expTime", "MP", "Lip",
"Cat", "CI"), class = "data.frame", row.names = c(NA, -24L))
希望重建我的代码会有所帮助。 再次提出我的问题:是的,我想在其中一个轴上显示曝光时间(如果可能的话)。其次,我想展示一种“时间序列”(从0h到672h)以及所有治疗组的MP和Lip的行为。所以我的第一个想法是:y轴:MP,x轴底部:唇部,顶部的x轴:曝光时间 - &gt;按照正确的顺序绘制所有治疗组的暴露时间值(从0到672)。尝试绘制趋势线。事实上,我想要一个视觉证据,MP行为(随着时间的推移)导致不同治疗组的脂褐素累积变化。
@Jake Kaupp:我不确定,如何在ggplot中facet_wrap
。你可以指一下吗?