ggplot2是一个积极维护的R开源图表绘图包,由Hadley Wickham编写,基于“图形语法”的原则。它部分取代了R的基本图和格子包,同时提供了一个干净,强大,正交和有趣的API。
在 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")
如何在geom_raster中通过二元密度设置alpha并通过z填充?
我用 geom_raster 制作了一个热图,如下所示: 圆<- function(x,base){base*round(x/base)} # round to nearest 5 df <- data.frame(x= mround(rnorm(10000, 100, 10), 5), ...
我的图表显示 x 轴上现有值范围之外的文本。 为了使文本处于正确的 y 级别,我使用 coord_cartesian 增加了 x 轴限制。 我想删除一部分...
我正在尝试绘制如下图所示的内容,其中 x 轴是 InEx 和 ReA SFMC,但它们是称为“Group”的变量的两个类别,y 轴是称为“n”的变量(
我正在使用 ggplot2 绘制一些图来表示 Likert 量表数据,并且需要排斥一些标签,但不需要排斥其他标签。在 StackOverflow 上找到大量答案之后,我想出了......
我已经使用ggplot2绘制了冲积图,但是我似乎无法弄清楚如何仅对变量“CTaa_alpha”中最常见的对“CAGGFNYQLIW”进行着色,这是
如何全局定义renderDataTable的设置,类似于我们在ggplot中处理主题?
我不知道如何让我的代码更容易理解,因为我闪亮的服务器代码的很大一部分是这样的: 输出$t1_summary <- renderDataTable( { table_somethign() } , ...
在 R 中,我正在创建不同版本的地图,使用相同的国家/地区但不同的权重因子值。 在我的示例中,我创建了两个地图,其中第二个地图有双...
如何使用 ggcuminc 或 survfit 将风险表图例文本和标题移动到图中的左侧
我希望将风险表标题和风险表文本定位并对齐到最左侧,如第一张图片所示。 暨公司情节的目的 我创建了一个对象: 图书馆(苏...
我正在尝试更改使用facet_wrap()绘制的两个图的标题,其中一个具有大于或等于符号(>=)。我可以得到图表来解析标题,但是一旦我输入 &g...
如何在水平条形图中显示数百万到“M”或数千到“K”的后缀而不是完整数字?
我想在我的条形图中添加“M”或“K”后缀。每个标签的数字显示完整的原始数字,但我想四舍五入到最接近的千位并显示...
首先,使用的代码。 ggplot(相关性, aes(x=area_ha, y=extent_2000_ha)) + geom_point( color="green") + theme_ipsum() + 主题(text=element_text(family="Times New Roman", ...