r - 使用 ggplot2 添加颜色和位置闪避以选择图中的年份

标签 r ggplot2

我有一些数据: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))

我可以生成下图:

enter image description here

问题在于 2010 年是基准年。为了以图形方式表示这一点,我希望 2010 年用灰色符号和灰色错误栏表示,但我无法让它工作!我希望该符号与其他年份使用的符号不同,但 2010 年的所有治疗方法都相同(与其他年份不同)。有谁知道这样做的方法吗?

我还想避开 2011 年、2012 年和 2013 年(但不是 2010 年)绘制数据的位置。我过去使用 position=dodge 效果很好,但今天它对我不起作用,因为 R 已经决定它找不到函数“dodge”!如果有人知道如何克服这个问题以及如何只避开那些特定年份的数据,我会很高兴听到它!

非常感谢。

最佳答案

由三个不同数据集构建的图:
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)

enter image description here

关于r - 使用 ggplot2 添加颜色和位置闪避以选择图中的年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18579096/

相关文章:

R-建立我自己的CRAN储存库

r - 用向量值绘制图形

r - 在等高线图ggplot2中绘制点

r - 更改 ggplot facet_wrap 的构面标题格式

r - 无法在 R 中加载新的 ggplot2

r - 如何在水平条形图中的条形右侧添加计数标签?

r - 一个图表中的条形图和线图,在 ggplot2 下带有图例

r - 如何在 R 中将数据与特定范围内的平线拟合?

r - 在空的 ggplot 中获取图例着色

c++ - R 矩阵到 Armadillo 的转换真的很慢