ggplot2是一个积极维护的R开源图表绘图包,由Hadley Wickham编写,基于“图形语法”的原则。它部分取代了R的基本图和格子包,同时提供了一个干净,强大,正交和有趣的API。
使用 ggplot 更改 stat_compare_means() 在图中的位置
我有这个带有 ggpubr::stat_compare_means() 函数的 ggplot 图: 正如您所看到的,Wilcoxon 测试与左侧条上的一些点重叠。 如何将文本移到右侧?
我试图用一条线连接点,但我已按多种条件对点进行分组。到目前为止我的代码如下: ggplot(响应数据, aes(x = 时间,y = Y,组 = 基因型,颜色 =
如何使用 ggplot2 在具有离散 x 刻度的分组箱线图之间移动刻度和标签?
当然已经问过,但我正在努力寻找适合我的情况的明确解决方案。 如何在分组箱线图之间移动刻度(而不是以它们为中心)以便定义小时间隔? 在...
ggplot:stat_contour_filled结合ggpattern根据一定条件进行区域填充
例如,我想使用ggpattern的条纹来填充值大于50的区域。但是breaks参数(可能是我的问题)没有达到我想要的结果。 图书馆(tidyverse) d &...
将 geom_scatterpie 添加到 geom_tile - 将 geom 转换为 grob 时出现问题
我无法在使用 get_tiles 和 geom_tile 制作的 ggplot 图层上使用 geom_scatterpie。我总是收到以下错误: 错误: !将 geom 转换为 grob 时出现问题。 ℹ 错误发生在 那个...
我有一个包含 24 列的 tibble,每列有 107 个观察值。每列都是一个描述 CT 图像中骨骼样本的变量,列中的所有值都是“双精度”类型...
R - 来自 tibble 列的直方图:为什么这对某些列有效,而对其他列无效?
我有一个包含 24 列的 tibble,每列有 107 个观察值。每列都是描述 CT 图像中骨骼样本的变量,列中的所有值均为“双精度”类型...
我尝试过使用各种不同的方法来为我的 4 个 ggplots 创建一个组合图例,但是,它会导致其中一个图表与另一个图表相比被压扁,因为
我想创建一个水平排列的堆积条形图。 条形图应显示“cost1”和“cost2”的总和,并在每个条形图的末尾有一个标签。下面,您可以看到
我正在使用 ggsurvfit 包来创建 KM 图。我有一个分组变量,并希望为该变量添加图例。 请检查下面的代码:我正在寻找为此特定添加图例
我正在尝试在 R 中为微生物家族中的物种制作热图,样本 ID 为 x,物种名称为 y,顶部有两个分面因子组。我想要像图片一样的东西...
我正在尝试将森林图与值的数据表对齐。数据表在最后一个数据点之前结束。我的代码和带有虚拟标签的绘图如下。感谢您的帮助!! <- ...
在 R 中使用 ggbreak 分割 y 轴时,来自 geom_text_repel 的重复标签
我有一个由几千个点组成的散点图,这些点由一个使用facet_wrap的因子分面。我已经使用 ggbreak 包中的scale_y_break成功分割了y轴,现在我想标记一个
我有一些数据正在绘制,如下所示: 克<- ggplot(datapile, aes(x = Re, y = CD)) g <- g + geom_line(data = datapile, linetype = "solid", size = 1) g <- g + scale_y_continuous(...
如何在 R 中使用 `ggplot2` 绘制 Log Pearson III 分布?
我正在将 Log Pearson III 分布拟合到我的水流数据中。拟合后,我想绘制观测值和拟合分布。 然而,这个数字并不是我所期望的: 1. ...
如何使用 PCA 计算的值作为 R 中轴的 ggplot 标签
当我在 R 中运行主成分分析,然后在 ggplot 中绘制 2 个 PC 时,我希望能够让轴标签自动包含轴上的 PC # 以及
我使用这个代码: 库(sjPlot) mtcars |> 绘图_frq(圆柱) 我得到 我想删除括号并将百分比数字四舍五入到最接近的整数。这样我就得到了 34%...
gghighlight 和facet_wrap 始终突出显示
第一次发帖,如有错误还请见谅! 我正在尝试使用 gghighlight 包在 R 中的 ggplot2 中制作一个小的倍数图。我有多个地点随时间变化的温度...
使用 R 中的 ggplot2 自动调整条形图上每个条形内文本的大小
我在 R 中有一个名为 df 的数据框,如下所示: df # 小题:15 × 3 # 组:var1 [3] var1 var2 每 我在 R 中有一个名为 df 的数据框,看起来像这样: df # A tibble: 15 × 3 # Groups: var1 [3] var1 var2 per <chr> <fct> <dbl> 1 My own "Very \n Dissatisfied" 0.00751 2 My own "Dissatisfied" 0.0641 3 My own "Neutral" 0.353 4 My own "Satisfied" 0.384 5 My own "Very \n Satisfied" 0.192 6 No "Very \n Dissatisfied" 0.0445 7 No "Dissatisfied" 0.135 8 No "Neutral" 0.417 9 No "Satisfied" 0.273 10 No "Very \n Satisfied" 0.130 11 Yes "Very \n Dissatisfied" 0.0233 12 Yes "Dissatisfied" 0.0639 13 Yes "Neutral" 0.280 14 Yes "Satisfied" 0.340 15 Yes "Very \n Satisfied" 0.293 我想将其绘制如下图所示,但我希望在每个图中自动调整每个条形中间的百分比。但这里有重叠。 ggplot(df,aes(x=var1,y=per,fill=var2))+ geom_col(position = position_fill(reverse = TRUE))+ theme(axis.title.y=element_blank(),axis.title.x=element_blank())+ coord_flip()+ scale_fill_brewer(palette ="RdYlGn",direction = 1,type="div")+ theme(axis.text.y=element_text(size=12, angle=0,hjust=0,vjust=0))+ theme(axis.text.x=element_text(size=12, angle=0,hjust=0,vjust=0)) + theme(legend.text=element_text(size = 15), legend.title = element_blank() )+ scale_y_continuous(labels = scales::percent)+ geom_text(aes(label = scales::percent(round(per,2))), position = position_stack(reverse = TRUE, vjust = .5)) 我怎样才能在 R 中做到这一点? 数据 structure(list(var1 = c("My own", "My own", "My own", "My own", "My own", "No", "No", "No", "No", "No", "Yes", "Yes", "Yes", "Yes", "Yes"), var2 = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), levels = c("Very \n Dissatisfied", "Dissatisfied", "Neutral", "Satisfied", "Very \n Satisfied"), class = "factor"), per = c(0.00751285092922104, 0.0640569395017794, 0.352708580466588, 0.383550810597074, 0.192170818505338, 0.0445205479452055, 0.134703196347032, 0.417237442922374, 0.273401826484018, 0.13013698630137, 0.0233160621761658, 0.0639032815198618, 0.27979274611399, 0.340241796200345, 0.292746113989637)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -15L), groups = structure(list( var1 = c("My own", "No", "Yes"), .rows = structure(list(1:5, 6:10, 11:15), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame" ), row.names = c(NA, -3L), .drop = TRUE)) 我认为最好将过度绘制的标签删除到不同的高度。我不知道如何快速使用 position_dodge,所以我建议采用更手动的方法。 library(ggplot2) library(dplyr) df <- df |> group_by(var1) |> mutate(cper = cumsum(per) - per/2) ggplot(df,aes(x=var1,y=per,fill=var2, group = var2))+ geom_col(position = position_fill(reverse = TRUE))+ theme(axis.title.y=element_blank(),axis.title.x=element_blank())+ coord_flip()+ scale_fill_brewer(palette ="RdYlGn",direction = 1,type="div")+ theme(axis.text.y=element_text(size=12, angle=0,hjust=0,vjust=0))+ theme(axis.text.x=element_text(size=12, angle=0,hjust=0,vjust=0)) + theme(legend.text=element_text(size = 15), legend.title = element_blank() )+ scale_y_continuous(labels = scales::percent)+ geom_text(data = df[df$var2 != "Very \n Dissatisfied",], aes(y = cper, label = scales::percent(round(per,2)))) + geom_label(data = df[df$var2 == "Very \n Dissatisfied",], aes(y = cper, label = scales::percent(round(per,2))), nudge_x = 0.15, show.legend = FALSE)
我有一个名为 df 的数据框,它有 3 个 Likert 量表级别列和一个过滤器列: df # 小标题:50 × 4 val1 val2 val3 var 我有一个名为 df 的数据框,它有 3 个 Likert 量表级别列和一个过滤器列: df # A tibble: 50 × 4 val1 val2 val3 var <chr> <chr> <chr> <chr> 1 "Very \n Dissatisfied" "Neutral" "Very \n Dissatisf… Yes 2 "Neutral" "Neutral" "Neutral" No 3 "Dissatisfied" "Satisfied" "Neutral" Yes 4 "Very \n Satisfied" "Satisfied" "Very \n Satisfied" Yes 5 "Very \n Dissatisfied" "Very \n Dissatisfied" "Neutral" Yes 6 "Very \n Satisfied" "Very \n Satisfied" "Very \n Satisfied" Yes 7 "Dissatisfied" "Neutral" "Dissatisfied" Yes 8 "Neutral" "Satisfied" "Neutral" Yes 9 "Satisfied" "Very \n Satisfied" "Satisfied" No 10 "Neutral" "Satisfied" "Neutral" Yes 上一个问题的结果函数这里 给了我所有相同值的条形图。这是正确的。!!我想要的只是不要重复 3 次(20,30 和 50),我想在右图的这一栏上重复一次。不是 3 次。 这可能吗? plot_fun <- function(x, y) { .data <- df |> filter(var %in% x) p1 <- .data |> ggstats::gglikert(include = -var) + aes(y = reorder(.question, ifelse( .answer %in% c("Very \n Dissatisfied", "Dissatisfied"), 1, 0 ), FUN = sum ), decreasing = TRUE) + facet_wrap(~paste0("var to ", y))+ scale_fill_manual(values = custom_colors) + theme( strip.text = element_text(size = 14,color = "black"), # Increase facet label size axis.title = element_text(size = 14), # Increase axis title size axis.text = element_text(size = 10))+ # Increase axis text size theme(strip.background = element_rect(color="black", fill="red", size=1.5, linetype="solid")) p2 <- .data %>% tidyr::pivot_longer(-var) |> filter(!is.na(value)) |> mutate( name = reorder(name, ifelse( value %in% c("Very \n Dissatisfied", "Dissatisfied"), 1, 0 ), FUN = sum ) ) |> ggplot(aes(y = name)) + geom_bar(fill = "lightgrey")+ theme_light()+ geom_text(aes(label = ..count..), stat = "count", position=position_stack(vjust = 0.5))+ theme( axis.text.y = element_blank(), axis.ticks.y = element_blank()) list(p1, p2) } .include <- list(No = "No", Yes = "Yes", All = c("Yes", "No")) purrr::imap(.include, plot_fun) |> purrr::reduce(c) |> wrap_plots(ncol = 2) + plot_layout(axes = "collect", guides = "collect", widths = c(.7, .3)) & labs(x = NULL, y = NULL) & theme(legend.position = "bottom") 数据 dput(df) structure(list(val1 = c("Very \n Dissatisfied", "Neutral", "Dissatisfied", "Very \n Satisfied", "Very \n Dissatisfied", "Very \n Satisfied", "Dissatisfied", "Neutral", "Satisfied", "Neutral", "Very \n Dissatisfied", "Very \n Satisfied", "Very \n Dissatisfied", "Satisfied", "Neutral", "Very \n Dissatisfied", "Neutral", "Neutral", "Satisfied", "Neutral", "Very \n Satisfied", "Dissatisfied", "Dissatisfied", "Satisfied", "Neutral", "Dissatisfied", "Satisfied", "Very \n Dissatisfied", "Dissatisfied", "Very \n Dissatisfied", "Very \n Dissatisfied", "Dissatisfied", "Dissatisfied", "Dissatisfied", "Neutral", "Dissatisfied", "Dissatisfied", "Very \n Dissatisfied", "Satisfied", "Satisfied", "Neutral", "Very \n Dissatisfied", "Very \n Satisfied", "Very \n Dissatisfied", "Satisfied", "Very \n Dissatisfied", "Very \n Dissatisfied", "Satisfied", "Dissatisfied", "Dissatisfied"), val2 = c("Neutral", "Neutral", "Satisfied", "Satisfied", "Very \n Dissatisfied", "Very \n Satisfied", "Neutral", "Satisfied", "Very \n Satisfied", "Satisfied", "Very \n Dissatisfied", "Very \n Satisfied", "Satisfied", "Very \n Satisfied", "Satisfied", "Neutral", "Dissatisfied", "Satisfied", "Neutral", "Satisfied", "Satisfied", "Neutral", "Very \n Satisfied", "Very \n Satisfied", "Satisfied", "Satisfied", "Very \n Satisfied", "Satisfied", "Neutral", "Neutral", "Neutral", "Neutral", "Neutral", "Satisfied", "Satisfied", "Dissatisfied", "Neutral", "Satisfied", "Very \n Satisfied", "Satisfied", "Satisfied", "Very \n Dissatisfied", "Satisfied", "Neutral", "Satisfied", "Very \n Dissatisfied", "Neutral", "Satisfied", "Neutral", "Satisfied" ), val3 = c("Very \n Dissatisfied", "Neutral", "Neutral", "Very \n Satisfied", "Neutral", "Very \n Satisfied", "Dissatisfied", "Neutral", "Satisfied", "Neutral", "Very \n Dissatisfied", "Very \n Satisfied", "Very \n Dissatisfied", "Satisfied", "Neutral", "Very \n Dissatisfied", "Satisfied", "Neutral", "Satisfied", "Neutral", "Very \n Satisfied", "Neutral", "Satisfied", "Satisfied", "Neutral", "Dissatisfied", "Satisfied", "Very \n Satisfied", "Neutral", "Very \n Dissatisfied", "Very \n Dissatisfied", "Dissatisfied", "Satisfied", "Dissatisfied", "Dissatisfied", "Very \n Dissatisfied", "Dissatisfied", "Very \n Dissatisfied", "Satisfied", "Satisfied", "Neutral", "Very \n Dissatisfied", "Very \n Satisfied", "Very \n Dissatisfied", "Satisfied", "Very \n Dissatisfied", "Dissatisfied", "Satisfied", "Neutral", "Dissatisfied"), var = c("Yes", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No", "No", "Yes", "No", "No", "No", "No", "No", "Yes", "No", "No", "Yes", "No", "No", "No", "Yes", "No", "No", "Yes", "No", "No", "No", "No", "No", "Yes", "No", "No", "No", "Yes", "No", "No", "Yes", "Yes", "No", "Yes", "Yes", "No", "No", "No", "Yes" )), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame" )) likert_levels <- c( "Strongly disagree", "Disagree", "Neither agree nor disagree", "Agree", "Strongly agree" ) 只需拆下枢轴部分即可: library(tidyverse) library(patchwork) likert_levels <- c( "Very \n Dissatisfied", "Dissatisfied", "Neutral", "Satisfied", "Very \n Satisfied" ) plot_fun <- function(x, y) { .data <- df |> filter(var %in% x) |> mutate( across(-var, ~ factor(.x, likert_levels)) ) p1 <- .data |> ggstats::gglikert(include = -var) + aes(y = reorder(.question, ifelse( .answer %in% c("Very \n Dissatisfied", "Dissatisfied"), 1, 0 ), FUN = sum ), decreasing = TRUE) + facet_wrap(~ paste0("var to ", y)) + # scale_fill_manual(values = custom_colors) + theme( strip.text = element_text(size = 14, color = "black"), # Increase facet label size axis.title = element_text(size = 14), # Increase axis title size axis.text = element_text(size = 10) ) + # Increase axis text size theme(strip.background = element_rect(color = "black", fill = "red", size = 1.5, linetype = "solid")) p2 <- .data %>% count() |> ggplot(aes(y = factor(1), x = n)) + geom_col(fill = "lightgrey") + theme_light() + geom_text(aes(label = n), position = position_stack(vjust = 0.5) ) + theme( axis.text.y = element_blank(), axis.ticks.y = element_blank() ) list(p1, p2) } .include <- list(No = "No", Yes = "Yes", All = c("Yes", "No")) purrr::imap(.include, plot_fun) |> purrr::reduce(c) |> wrap_plots(ncol = 2) + plot_layout(guides = "collect", widths = c(.7, .3)) & labs(x = NULL, y = NULL) & theme(legend.position = "bottom")