我有一些数据:http://pastebin.com/DyXB6NFq
并使用以下代码:
A<-ggplot(data = Trial, aes(x = as.factor(Year), y = DV,group = TMT, shape=TMT, col = TMT, )) +
geom_point(size = 3) +
geom_errorbar(aes(ymin=trial$DV-Trial$ErrB, ymax=Trial$DV+Trial$ErrB, width = 0.1)) +
geom_line(linetype=3) +
ylab("Proportion Y") +xlab("Year") +
theme(axis.title.y=element_text(size=rel(1.1),vjust=0.2),axis.title.x=element_text(size=rel(1.1),vjust=0.2),axis.text.x=element_text(size=rel(1)),axis.text.y=element_text(size=rel(1)),text = element_text(size=13))+scale_colour_brewer(palette="Set1")+scale_colour_manual(name = "Tmt",
labels = c("D", "C", "B", "A"),
values = c("red", "red","blue", "blue")) +
scale_shape_manual(name = "Tmt",
labels = c("D", "C", "B", "A"),
values = c(19, 17,19, 17))
我可以生成以下图表:
问题是2010年代表基准年。为了以图形方式表示,我希望2010用灰色符号代表灰色错误条,但我无法让它工作!我希望该符号与其他年份使用的符号不同,但对于2010年的所有治疗都是一样的(与其他年份不同)。有谁知道这样做的方法?
我还想躲避2011年,2012年和2013年(但不是2010年)绘制数据的位置。我过去使用过position =闪避效果很好,但是今天它不适合我,因为R已经决定它找不到“闪避”这个功能!如果有人知道如何克服这一点,以及如何只选择那些选定年份的数据,我会很高兴听到它!
非常感谢提前。
答案 0 :(得分:4)
根据三个不同的数据集构建的图:
1.多年的点和线> 2010.躲闪,红色和蓝色
2. 2010年的积分。没有回避,灰色
3. 2010年至2011年之间的界限
# create data for years > 2010
trial2 <- Trial[Trial$Year > 2010, c("DV", "Year", "ErrB", "TMT")]
# create data for points and error bars 2010
trial2010 <- Trial[Trial$Year == 2010, c("DV", "Year", "ErrB", "TMT")]
# plot points and lines for years > 2010, with dodging
pd <- position_dodge(width = 0.2)
g1 <- ggplot(data = trial2, aes(x = Year, y = DV, col = TMT)) +
geom_point(aes(shape = TMT), size = 3, position = pd) +
geom_errorbar(aes(ymin = DV - ErrB, ymax = DV + ErrB), width = 0.1, position = pd) +
geom_line(aes(group = TMT), linetype = 3, position = pd) +
scale_colour_manual(name = "TMT", labels = c("A", "B", "C", "D"),
values = c("red", "red","blue", "blue")) +
scale_shape_manual(name = "TMT",
labels = c("A", "B", "C", "D"),
values = c(19, 17, 19, 17)) +
coord_cartesian(xlim = c(2009.8, 2013.2)) +
theme_classic()
g1
# create data for lines between 2010 and 2011
# get x- and y-coordinates for the dodged 2011 points from plot object, i.e. first 4 rows
trial2011 <- ggplot_build(g1)[["data"]][[1]][1:4, c("x", "y")]
names(trial2011) <- c("Year", "DV")
# add TMT
trial2011$TMT <- LETTERS[1:4]
# combine data for 2010 and 2011
trial1011 <- rbind(trial2010[ , c("Year", "DV", "TMT")], trial2011)
# add points and error bars for 2010, and lines 2010-2011 to plot
# no dodging
pd0 <- position_dodge(width = 0)
g1 + geom_point(data = trial2010,
aes(x = Year, y = DV), col = "grey", shape = 19, size = 4, position = pd0) +
geom_errorbar(data = trial2010,
aes(ymin = DV - ErrB, ymax = DV + ErrB), col = "grey", width = 0.1, position = pd0) +
geom_line(data = trial1011, aes(group = TMT), linetype = 3)