[英]Remove select labels from plot ggplot2
這聽起來很奇怪,但我需要從我正在制作的條形字符中的兩個不同條形中刪除標簽。 我已經閱讀了一些關於過濾值的方法的帖子,例如這里。 但是,我需要刪除的兩個值彼此大不相同,所以我認為這種技術行不通。 讓我給你我正在使用的代碼、一個示例數據集,以及我正在尋找的一些圖片。 我想要做的是從圖中刪除 130 和 1。 在第一張圖中,這兩個數字都用紅色圈出。
注意:示例數據集中的值可能看起來很小,但是在geotext()
,標簽乘以 310
示例代碼:
ggplot() +
geom_bar(data=data.bw, aes(x = num, y = bar),position = position_dodge(),stat = 'identity', width = 0.75,fill = "#D0D3D4", alpha=0.5) +
geom_text(data=data.bw, aes(x= num, y = bar,label = bar*310), vjust = -0.2, size=4.5) +
scale_y_continuous(
#minor_breaks = seq(0, 1, by = 50),
breaks = seq(0, 1), limits = c(0, 1),
expand = expansion(mult = c(0, 0)),
#guide = "axis_minor"
) +
scale_x_continuous(
#minor_breaks = seq(0, 25, by = 5),
#breaks = seq(0, 25, by = 2), limits = c(0, 25),
expand = expansion(mult = c(0, 0)),
#guide = "axis_minor"
) +
guides(x.sec = "axis_minor", y.sec = "axis_minor") +
scale_fill_manual(values = alpha(c("red"), .3)) +
ylab("Probability") +
xlab("Number") +
theme_bw() +
guides(color = guide_legend(override.aes = list(shape = c(9,8)))) +
theme(
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x.top = element_blank(),
axis.text.y.right = element_blank(),
axis.text.x = element_text(color = "grey20", size = 12, hjust = .5, vjust = .5, face = "plain"),
axis.text.y = element_text(color = "grey20", size = 12, hjust = .5, vjust = 0.2, face = "plain"),
axis.title.x = element_text(color = "grey20", size = 14, hjust = .5, vjust = 0, face = "plain"),
axis.title.y = element_text(color = "grey20", size = 16, hjust = .5, vjust = .5, face = "plain"),
plot.margin = margin(20, 3, 10, 3),
legend.title = element_blank(),
legend.text=element_text(size=12)
)
示例數據:
structure(list(num = c(1, 1.75, 2.5, 4, 4.75, 5.5, 6.25, 7, 10,
13.75, 15.25, 16, 17.5, 18.25, 19, 19.75, 20.5, 22, 22.75, 23.5
), bar = c(0.948387096774194, 0.419354838709677, 0.145161290322581,
0.0419354838709677, 0.0161290322580645, 0.00645161290322581,
0.0032258064516129, 0.0032258064516129, 0.0032258064516129, 0.0032258064516129,
0.00967741935483871, 0.0193548387096774, 0.0354838709677419,
0.067741935483871, 0.112903225806452, 0.125806451612903, 0.148387096774194,
0.132258064516129, 0.0516129032258065, 0.00967741935483871),
num2 = c(294L, 130L, 45L, 13L, 5L, 2L, 1L, 1L, 1L, 1L, 3L,
6L, 11L, 21L, 35L, 39L, 46L, 41L, 16L, 3L)), row.names = c(NA,
-20L), class = "data.frame")
我想刪除的值的當前圖以紅色圈出:
想要的劇情:
為什么不直接將過濾后的數據幀發送到geom_text
的data
參數?
ggplot() +
geom_bar(data=data.bw, aes(x = num, y = bar),
position = position_dodge(),stat = 'identity', width = 0.75,fill = "#D0D3D4", alpha=0.5) +
geom_text(data=data.bw[!data.bw$num2 %in% c(130, 1),],
aes(x= num, y = bar,label = num2), vjust = -0.2, size=4.5) +
scale_y_continuous(
breaks = seq(0, 1), limits = c(0, 1),
expand = expansion(mult = c(0, 0)),
) +
scale_x_continuous(
expand = expansion(mult = c(0, 0)),
) +
scale_fill_manual(values = alpha(c("red"), .3)) +
ylab("Probability") +
xlab("Number") +
theme_bw() +
guides(color = guide_legend(override.aes = list(shape = c(9,8)))) +
theme(
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x.top = element_blank(),
axis.text.y.right = element_blank(),
axis.text.x = element_text(color = "grey20", size = 12, hjust = .5, vjust = .5, face = "plain"),
axis.text.y = element_text(color = "grey20", size = 12, hjust = .5, vjust = 0.2, face = "plain"),
axis.title.x = element_text(color = "grey20", size = 14, hjust = .5, vjust = 0, face = "plain"),
axis.title.y = element_text(color = "grey20", size = 16, hjust = .5, vjust = .5, face = "plain"),
plot.margin = margin(20, 3, 10, 3),
legend.title = element_blank(),
legend.text=element_text(size=12)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.