[英]How do I align geom_dotplot to value of the x axis instead of having them in the center of the axis?
[英]ggplot2 - how to align geom_dotplot with x-axis labels?
我使用 ggplot2 在下面生成了点 plot。 然而,这些点与它们各自的实验不一致(第一个实验除外)。 我如何让它们对齐?
这是我使用的脚本:
emibc_plot <- ggplot(emibc, aes(x = experiment, y = value, fill = model)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.title=element_blank()) +
scale_y_continuous(breaks = sort(c(seq(min(emibc$value), max(emibc$value), length.out=5), 0)), label= function(x) {ifelse(x==0, "0", scales::scientific_format(digits = 3)(x))}) +
geom_dotplot(binaxis = "y", stackdir = "center", stackgroups=TRUE, binpositions="all", dotsize = 0.5)
这是dput(emibc)
的 output :
structure(list(model = c("CESM", "E3SM", "GISS", "MIROC", "CESM",
"E3SM", "GISS", "MIROC", "CESM", "E3SM", "GISS", "MIROC", "CESM",
"E3SM", "MIROC", "CESM", "E3SM", "GISS", "MIROC"), experiment = c("bc_no_seas",
"bc_no_seas", "bc_no_seas", "bc_no_seas", "high_SO4", "high_SO4",
"high_SO4", "high_SO4", "no_SO4", "no_SO4", "no_SO4", "no_SO4",
"SO2_at_height", "SO2_at_height", "SO2_at_height", "SO2_no_season",
"SO2_no_season", "SO2_no_season", "SO2_no_season"), value = c(2.13359e-16,
2.13549e-16, 1.04680e-17, 2.86707e-16,
3.97356e-20, 1.77632e-20, 0, 0, 2.22483e-20,
1.17037e-20, 0, 0, 2.51571e-20, 9.81121e-21,
0, 1.67484e-20, 1.46975e-20, 0, 0)), row.names = c(NA,
-19L), class = c("tbl_df", "tbl", "data.frame"))
可能值得注意的是,我在运行脚本时收到此警告:
`stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
ggplot2 的 GitHub 存储库中有一些未解决的问题; 见1745和3620 。 这里还有一个拉取请求(已关闭且未合并): 1096 。
不过,有一种 hacky 方法。 从 PR 1096和这个答案,您可以将fill
参数传递给geom_dotplot()
。 正如用户vitorbl 在这里提到的,您的数据似乎必须先排序。
# required libraries
library(ggplot2)
library(dplyr)
# OP's data
emibc <- structure(list(
model = c("CESM", "E3SM", "GISS", "MIROC", "CESM",
"E3SM", "GISS", "MIROC", "CESM", "E3SM",
"GISS", "MIROC", "CESM", "E3SM", "MIROC",
"CESM", "E3SM", "GISS", "MIROC"),
experiment = c("bc_no_seas", "bc_no_seas", "bc_no_seas",
"bc_no_seas", "high_SO4", "high_SO4",
"high_SO4", "high_SO4", "no_SO4",
"no_SO4", "no_SO4", "no_SO4", "SO2_at_height",
"SO2_at_height", "SO2_at_height", "SO2_no_season",
"SO2_no_season", "SO2_no_season", "SO2_no_season"),
value = c(2.13359e-16, 2.13549e-16, 1.04680e-17,
2.86707e-16, 3.97356e-20, 1.77632e-20,
0, 0, 2.22483e-20, 1.17037e-20, 0, 0,
2.51571e-20, 9.81121e-21, 0,
1.67484e-20, 1.46975e-20, 0, 0)),
row.names = c(NA, -19L),
class = c("tbl_df", "tbl", "data.frame"))
# order data
emibc <- emibc %>%
arrange(experiment, value)
# create a column with a color for each experiment
emibc <- emibc %>%
mutate(
color = case_when(
model == "CESM" ~ "red",
model == "E3SM" ~ "darkgreen",
model == "GISS" ~ "lightblue",
model == "MIROC" ~ "purple"
)
)
# make plot
ggplot(emibc, aes(x = experiment, y = value)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.title = element_blank()) +
scale_y_continuous(breaks = sort(c(seq(min(emibc$value), max(emibc$value),
length.out=5), 0)),
label = function(x) {ifelse(x==0, "0", scales::scientific_format(digits = 3)(x))}) +
geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5,
fill = emibc$color)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.