gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:47:37 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com