在我的示例中,我在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]")
答案 0 :(得分:1)
我玩了一些,这不是一个很好的技巧,但是也许足够好。您可以使用shape = 21
,这是一个可填充的形状。然后,您需要将填充色与背景色进行匹配,以使其看起来好像没有被填充。您的示例代码中有一些错字。我希望我能正确地修复它们。
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(data = Counts,
aes(x = Treatment,
y = Areatime)) + geom_jitter(aes(y = Areatime,
colour = Size,
shape = Size),
size = 3,
width = 0.3) + scale_shape_manual(values = c(0, 15),
name = "Areatime",
breaks = c("Large", "Small"),
labels = c("Large", "Small")) + scale_colour_manual(values = c(1, 2)) + geom_jitter(aes(y = Visits,
fill = Size),
shape = 21, # this shape we can fill, but will give a border
size = 3,
width = 0.3) + scale_y_continuous(sec.axis = sec_axis(~./3, name = "Relative Visits [%]"))+ scale_fill_manual(values = c("grey95", "red"),
name = "Visits",
breaks = c("Large", "Small"),
labels = c("Large", "Small")) + guides(colour = guide_legend(title = "Areatime")) + 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]")
这同时产生:
答案 1 :(得分:0)
太好了,谢谢!我只是将两者的颜色都改为了黑色,而不是红色,并且将背景设为相同的灰色,并再次在“访问”之后的第二个*3
中添加了geom_jitter
(由于打字错误,您忘记了) 。因此,解决方案是:
ggplot(data = Counts, aes(x = Treatment, y = Areatime)) +
geom_jitter(aes(y = Areatime, colour = Size, shape = Size), size = 3, width = 0.3) +
scale_shape_manual(values = c(0, 15), name = "Areatime",
breaks = c("Large", "Small"), labels = c("Large", "Small")) +
scale_colour_manual(values = c(1, 1)) +
geom_jitter(aes(y = Visits*3, fill = Size), shape = 21, size = 3, width = 0.3) +
scale_y_continuous(sec.axis = sec_axis(~./3, name = "Relative Visits [%]")) +
scale_fill_manual(values = c("gray95", "black"),name = "Visits",
breaks = c("Large", "Small"), labels = c("Large", "Small")) +
guides(colour = guide_legend(title = "Areatime")) +
theme(axis.text.x=element_blank(),
panel.background = element_rect(fill = "gray95"),
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]")