【发布时间】:2020-02-17 18:56:39
【问题描述】:
在我的示例中,我在 ggplot 中创建了第二个 y 轴。
我不想改变第二个 y 轴数据点的颜色,而是改变形状。
但是,当然,我得到了错误Scale for 'shape' is already present. Adding another scale for 'shape', which will replace the existing scale.
最后,我想为第二个 y 轴的“访问”设置一个“大”圆圈和一个“小”实心圆圈。
这是我的代码:
Treatment <- c(rep(c("T/T"), times = 6),rep(c("R/R"), times = 6))
Size <- c(rep(c("Large", "Large", "Large", "Small", "Small", "Small"), times = 2))
Areatime <- c(240, 220, 120, 60, 55, 75, 90, 45, 70, 115, 40, 30)
Visits <- c(30, 45, 45, 65, 55, 55, 25, 35, 45, 75, 65, 55)
Counts <- data.frame(Treatment, Size, Areatime, Visits)
Counts$Treatment <- factor(Counts$Treatment, levels=c("T/T","R/R"))
Counts$inter <- interaction(Counts$Treatment, Counts$Size)
str(Counts)
ggplot(Count, aes(x = Treatment, y = Areatime)) +
geom_jitter(aes(y = Areatime, shape = Size, width = 0.3)) +
scale_shape_manual(values = c(0, 15), name = "Areatime", breaks = c("Large", "Small"),
labels = c("Large", "Small")) +
geom_jitter(aes(y = Counts*3, colour = Size, width = 0.3)) +
scale_y_continuous(sec.axis = sec_axis(~./3, name = "Relative Visits [%]"))+
scale_colour_manual(values =c(1, 2), name="Visits", breaks=c("Large", "Small"),
labels=c("Large", "Small")) +
theme(axis.text.x=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_line("black")) +
facet_grid(.~Treatment, scales="free", switch = "x") +
labs(y = "Average Areatime [sec]")
【问题讨论】:
-
您能否提供一个可重现的数据集示例? (见:stackoverflow.com/questions/5963269/…)
-
我编辑了原始帖子并添加了一个可重现的示例。