我有一些数据
sample <- data.frame(inst=c('School1', 'School1', 'School1', 'School1', 'School2', 'School2', 'School2', 'School2', 'School3', 'School3', 'School3', 'School3'), variable=c('Math_25', 'Math_75', 'Reading_25', 'Reading_75', 'Math_25', 'Math_75', 'Reading_25', 'Reading_75', 'Math_25', 'Math_75', 'Reading_25', 'Reading_75'), peer_min=c(50, 84, 61, 83, 40, 60, 55, 85, 52, 75, 75, 87), peer_max=c(66, 95, 77, 90, 55, 85, 72, 91, 67, 83, 84, 95), peer_mean=c(58.0, 89.5, 69.0, 86.5, 47.5, 72.5, 63.5, 88.0, 59.5, 79.0, 79.5, 91.0), inst_value=c(55, 93, 65, 95, 60, 70, 65, 80, 60, 85, 77, 89))
以及带有一些HTML颜色的ggplot:
my_red <- c("#AF272F")
my_black <- c("#101820")
my_grey<- c("#D0D0CE")
library(ggplot2)
plot <- ggplot(data=sample,aes(x=inst, y=peer_mean, shape=15)) + geom_errorbar(aes(ymin=peer_min, ymax=peer_max), colour=my_grey, width=0.5, size=2) + facet_wrap(~variable, scales="free_x")+ theme(panel.background=element_blank()) + ggtitle("this is my title") + theme(plot.title=element_text(hjust=0.5, size=15, face = "bold")) + theme(strip.text=element_text(size=12, face="bold")) + geom_point(size=3) + geom_point(aes(x=inst, y=inst_value, shape=8), colour=my_red, size=4) + scale_shape_identity() + theme(axis.title.x = element_blank()) + theme(axis.title.y = element_blank())
我试图通过黑色方块(标记为&#34; Inst Mean&#34;)和红色星标(标记为&#34;机构分数&#34;)来获得传奇。
我看到了这个helpful question并试图将它应用到我的情节中:
plot <- plot + theme(legend.position="bottom") + scale_shape_manual(name = "Legend Title", values=c(15,8)) + scale_colour_manual(name = "Legend Title", values=c("my_black", "my_red"))
我收到了这个错误:
Error: Continuous value supplied to discrete scale
答案 0 :(得分:2)
一种解决方案是创建&#34;虚拟&#34;映射。对于Institutional Score
我们映射fill
和Inst Mean
,我们可以映射shape
。
library(ggplot2)
ggplot(sample,aes(inst, peer_mean)) +
geom_errorbar(aes(ymin = peer_min, ymax = peer_max),
colour = my_grey, width = 0.5, size = 2) +
geom_point(aes(fill = "Institutional Score", y = inst_value, ),
shape = 8, colour = my_red, size = 4) +
geom_point(aes(shape = "Inst Mean"), size = 3) +
facet_wrap(~ variable, scales = "free_x") +
scale_shape_manual(values = 15) +
labs(title = "this is my title",
fill = NULL,
shape = NULL) +
theme(panel.background=element_blank(),
plot.title=element_text(hjust=0.5, size=15, face = "bold"),
strip.text=element_text(size=12, face="bold"),
axis.title = element_blank())
PS:我有类似的答案here,但是当处理多个变量时,它会变得更复杂。
答案 1 :(得分:0)
重塑数据然后绘图。
library(tidyr)
# reshape from wide-to-long, so we call geom_point once
plotDat <- sample %>%
gather(key = "Group", value = "Mean", -c(inst, variable, peer_max, peer_min))
ggplot(data = plotDat, aes(x = inst, y = Mean, shape = Group, colour = Group)) +
facet_wrap(~variable, scales = "free_x") +
geom_errorbar(aes(ymin = peer_min, ymax = peer_max), colour = my_grey, width = 0.5, size = 2) +
geom_point(size = 3) +
scale_shape_manual(name = "Legend Title", values = c(8, 15)) +
scale_colour_manual(name = "Legend Title", values = c(my_red, my_black, my_grey)) +
ggtitle("this is my title") +
# Keep all theme in one theme()
theme(legend.position = "bottom",
panel.background = element_blank(),
plot.title = element_text(hjust = 0.5, size = 15, face = "bold"),
strip.text = element_text(size = 12, face = "bold"),
axis.title = element_blank())