简体   繁体   中英

Add colour and a position dodge to select years in a plot, using ggplot2

I have some data: http://pastebin.com/DyXB6NFq

and using the following code:

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))

I can produce the following graph:

在此输入图像描述

The problem is that 2010 represents a baseline year. To represent this graphically, I would like 2010 to be represented by a greyed out symbol and greyed out error bars but I can't get it to work! I would like the symbol to be different from those used in other years, but be the same for all treatments in 2010 (unlike the other years). Does anyone know of a way to do this?

I would also like to dodge the position of the plotted data from 2011, 2012 and 2013 (but not 2010). I have used position=dodge to great effect in the past, but today it is not working for me as R has decided it cannot find the function "dodge"! If anyone knows how to overcome this and also how to dodge only data for those select years, I would be glad to hear of it!

Many thanks in advance.

A plot built from three different data sets:
1. points and lines for years > 2010. dodged, red and blue.
2. points for 2010. not dodged, grey.
3. lines between 2010 and 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)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM