程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐??

开发过程中遇到ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐?的问题如何解决?下面主要结合日常开发的经验,给出你关于ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐?的解决方法建议,希望对你解决ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐?有所启发或帮助;

我使用 ggplot2 生成了下面的点图。然而,这些点并没有与它们各自的实验对齐(第一个除外)。我如何让它们对齐?

这是我使用的脚本:

@H_404_5@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)

这是@H_404_5@dput(emibC)的输出:

@H_404_5@structure(List(model = c("CESM","E3SM","GISS","MIROC","CESM","MIROC"),experiment = c("bc_no_seas","bc_no_seas","high_SO4","no_SO4","SO2_at_height","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,2.22483e-20,1.17037e-20,2.51571e-20,9.81121e-21,1.67484e-20,1.46975e-20,row.names = c(NA,-19L),class = c("tbl_df","tbl","data.frame"))

ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐?

值得注意的是,我在运行脚本时收到此警告:

@H_404_5@`staT_Bindot()` using `bins = 30`. Pick better value with `binwIDth`.

解决方法

ggplot2 的 GitHub 存储库中存在一些与此相关的未解决问题;参见 1745 和 3620。这里还有一个拉取请求(已关闭且未合并):1096。

不过,有一种黑客方法。从 PR 1096 和 this answer,您可以将 fill 参数传递给 geom_dotplot()。由于用户 vitorbl 提到了 here,您的数据似乎必须先排序

# required libraries
library(ggplot2)
library(dplyr)

# OP's data 
emibc <- structure(list(
  model = c("CESM","E3SM","GISS","MIROC","CESM","MIROC"),experiment = c("bc_no_seas","bc_no_seas","high_SO4","no_SO4","SO2_at_height","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,2.22483e-20,1.17037e-20,2.51571e-20,9.81121e-21,1.67484e-20,1.46975e-20,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),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)

ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐?

大佬总结

以上是大佬教程为你收集整理的ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐?全部内容,希望文章能够帮你解决ggplot2 - 如何将 geom_dotplot 与 x 轴标签对齐?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。