簡體   English   中英

使用ggplot2添加顏色和位置閃避以在繪圖中選擇年份

[英]Add colour and a position dodge to select years in a plot, using 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))

我可以生成以下圖表:

在此輸入圖像描述

問題是2010年代表基准年。 為了以圖形方式表示,我希望2010用灰色符號代表灰色錯誤條,但我無法讓它工作! 我希望該符號與其他年份使用的符號不同,但對於2010年的所有治療都是一樣的(與其他年份不同)。 有誰知道這樣做的方法?

我還想躲避2011年,2012年和2013年(但不是2010年)繪制數據的位置。 我過去使用過position =閃避效果很好,但是今天它不適合我,因為R已經決定它找不到“閃避”這個功能! 如果有人知道如何克服這一點,以及如何只選擇那些選定年份的數據,我會很高興聽到它!

提前謝謝了。

從三個不同數據集構建的圖:
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)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM