[英]How to add observation count (n) in ggplot2 scatter plot legend
Image of legend i would like to add to 我想添加的传奇图片
I would like to know how can i add a simple observation number (n) in legend of this scatter plot in ggplot2 我想知道如何在ggplot2中的此散点图的图例中添加一个简单的观察数(n)
library(readr)
library(ggplot2)
library(dplyr)
All.mutations.no.inserts <- read_csv("All mutations no inserts.csv")
All.mutations.no.inserts$Fungicide <- factor(All.mutations.no.inserts$Fungicide, levels = c("SDHI 1",
"SDHI 2",
"SDHI 3",
"SDHI 4",
"SDHI 5",
"SDHI 6",
"SDHI 7",
"SDHI 8",
"SDHI 9",
"SDHI 10",
"SDHI 11",
"SDHI 12"))
All.mutations.no.inserts$SDH.mutation <- factor(All.mutations.no.inserts$`SDH.mutation`)
ggplot(All.mutations.no.inserts, aes(x = Fungicide, y = EC50, color = SDH.mutation)) +
geom_point(size = 4) +
scale_y_log10() +
theme_minimal() +
theme(axis.text.x=element_text(angle = -90, hjust = 0),
axis.title.x=element_blank())
How should i modify my code? 我应该如何修改我的代码?
here's an example using dplyr
. 这是使用dplyr
的示例。 See the comments in the code. 请参阅代码中的注释。
library(dplyr)
library(ggplot2)
# sample data set
expand.grid(y = rnorm(20),
x = letters[1:5],
z = letters[6:10]) %>%
sample_frac(0.75) %>%
# add column n with counts for each group
add_count(z) %>%
# combine the group z and count n into one column
mutate(zn = paste0(z, ' (', n, ')')) %>%
# plot as you had
ggplot(aes(x, y, colour = zn)) +
geom_point() +
# rename the legend title
labs(colour = 'z (# obs)')
Created on 2019-02-06 by the reprex package (v0.2.1) 由reprex软件包 (v0.2.1)创建于2019-02-06
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.