我在ggplot2中使用geom_hline(yintercept = log(2000), linetype = "dotdash")
在地图中添加y = log(2000)
的水平线(附在下方)。
我想要的是在与虚线对应的y轴的左侧显示图例“log(2000)”。有什么想法吗?
情节的R代码如下:
library(ggplot2); library(scales)
set.seed(10)
varx <- rep(c("group1", "group2"), each = 3)
vary <- rep(c("A", "B", "C"), 2)
poi <- sample(20:30, 6)
upper <- sample(40:50, 6)
lower <- sample(1:10, 6)
dat <- data.frame(varx, vary, poi, upper, lower)
dat
# varx vary poi upper lower
#1 group1 A 25 43 2
#2 group1 B 23 42 6
#3 group1 C 29 45 3
#4 group2 A 30 50 4
#5 group2 B 20 44 1
#6 group2 C 21 47 10
pp <- ggplot(dat, aes(colour = varx, y = poi, x = vary))
limits <- aes(ymax = upper, ymin = lower)
pp + geom_point(aes(shape=varx), position = position_dodge(0.3), size = 2) +
## dodge make the lines not stack upon each other
geom_errorbar(limits, size = 1, width = 0.15, position = position_dodge(0.3)) +
theme_bw() + ## Get rid of the grey background
geom_hline(yintercept = log(2000), linetype = "dotdash") +
coord_cartesian(ylim = c(1, 60)) +
scale_shape_manual(values = c(17, 19)) +
theme(legend.justification= c(1, 0),
legend.position = c(1, 0.75),
legend.key = element_blank(),## Get rid of the legend box
legend.title = element_blank(),
legend.text = element_text(size = 10, face = "bold"),
legend.background = element_rect(fill=alpha(0.0001))) +
labs(x = NULL, y = NULL)
答案 0 :(得分:1)
pp + scale_y_continuous(breaks = sort(c(log(2000), seq(0, 60, 10))),labels=c(0,"log(2000)",seq(10,60,10)))