我是R的新手。以前,我已经能够使用plot (p1)
覆盖2个相同类型的图p1和p2。 plot (p2, add=T)
。
当我用条形图重叠显示所有个别点时,我正在努力解决因素的定义。
我可以根据需要单独绘制条形图。点图看起来像我想要它,但我意识到我使用错误的相位定义作为数字来强制R plot显示每个值,而不是默认为boxplot(就像我使用plot(my.df$cond, my.df$val)
时
有关正确定义变量类型或是否使用正确的条形图和绘图函数的任何提示,将不胜感激。非常感谢你。
shpad <- c(1,2,5,6,1,2,5,6,1,2,5,6,1,2,5,6)
my.df <- data.frame(val=c(0.0738,0.0518,0.002,0.0397,0.1452,0.1152,0.1774,0.0658,0.0218,0.0497,-0.0296,0.0653,0.0848,0.1296,0.1416,0.0923,
phase=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4),
sub=c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4),
cond=c("NsNm", "NsNm", "NsNm", "NsNm", "NsLm", "NsLm", "NsLm", "NsLm", "LsNm", "LsNm", "LsNm", "LsNm", "LsLm", "LsLm", "LsLm", "LsLm"))
avg <-tapply(my.df$val, my.df$phase, mean)
barplot(avg, border=NA, names.arg=c("NsNm", "NsLm", "LsNm", "LsLm"),col=c("blue","darkblue","red", "darkred"),ylab = "score",ylim=c(-0.03,0.25))
plot(my.df$phase, my.df$val, type="p", ylim=c(-0.03,0.25), ylab = "score", pch=shpad)
tl; dr:问题是,如果不是最后一行,我有plot(my.df$phase, my.df$val, type="p", ylim=c(-0.03,0.25), ylab = "score", pch=shpad, add=T)
,格式不一致。
答案 0 :(得分:2)
好吧,所以,我已经尝试了一点点来完成你想要的东西,但我能用基本绘图系统做的最好的就是:
除了最后一行(我用
替换)之外,这完全由您的上述代码行完成points(my.df$phase,my.df$val,type="p",pch=shpad)
但是,如果您想使用ggplot2
库保留相同的情节,我认为您可以做得更好。使用此代码:
library('ggplot2')
new.df <- data.frame(avg,phase=levels(factor(phase)))
ggplot(new.df) +
geom_bar(stat="identity",aes(x=levels(phase),y=avg, fill=c("NsNm","NsLm","LsNm","LsLm")))+
geom_point(aes(x=my.df$phase,y=my.df$val,shape=factor(shpad))) +
scale_x_discrete(name="Type",labels=c("NsNm","NsLm","LsNm","LsLm")) +
ylab("Score")
我没有调整颜色和点类型以及图例标题(不确定它们有多重要,但可以摆弄它们)。但是,您可以看到这可能会产生您的目标。