ggplot2是一个积极维护的R开源图表绘图包,由Hadley Wickham编写,基于“图形语法”的原则。它部分取代了R的基本图和格子包,同时提供了一个干净,强大,正交和有趣的API。
我有下面的 Q-Q 图 库(ggplot2) ggplot(data.frame('Val' = c(-0.6434733395, -0.0000935279, -1.2176148541, -0.4817091973, 1.1364799871, -0.8810002309, -0.8383323744, -0.7285213185, 1.62990681...
我在 R 中有一个数据框,经过一些数据转换、计算和绘图后: 图书馆(tidyverse) 库(ggstats) 图书馆(拼凑而成) 图书馆(小标题) 图书馆(tidyverse) 库(ggplot2)
“ggplot2”上的“position_dodge”和“geom_text_repel”不兼容
我在躲避图表上的 geom_text_repel 标签时面临问题。我在论坛上查看了其他类似的问题,但我无法准确理解如何强制回避这些标签。 ...
对 R 中 ggplot 2 中每个面网格上的 y 轴进行排序
我在 R 中有一个名为 X 的数据框。 X # 小标题:27 × 6 年份增量计数颜色标签类别 1 2024 -0....
我有下面的ggplot 库(ggplot2) ggplot (结构(列表(组1 = c('A','B', 'B','A', 'B','B', 'A', 'B', 'B'), Val = c (40.707、-22.513、-3.501、-12.884、-19.668、 -5.976, -16.721, -15.838, -5.59)...
在 R 中的 ggplot2 的 Likert 图中添加总计
我在 R 中有一个名为 df 的数据框: df # 小题:5 × 7 非常 不满意` 不满意 一般 满意`非常 满意` 我在 R 中有一个名为 df 的数据框: df # A tibble: 5 × 7 var n `Very \n Dissatisfied` Dissatisfied Neutral Satisfied `Very \n Satisfied` <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> 1 A 106 18.9 14.5 23.0 22.0 21.7 2 B 106 19.2 16.0 25.5 18.9 20.4 3 C 87 22.2 25.3 15.7 17.2 19.5 4 D 102 19.0 19.0 21.2 22.9 18.0 5 E 99 22.2 20.5 20.9 17.5 18.9 从这个数据框中,我创建了两个图,一个李克特图和一个条形图: df_long <- df %>% select(-n)%>% pivot_longer(!var, names_to = "Likert", values_to = "Percentage") # Likert plot likert_plot <- ggplot(df_long, aes(x = var, y = Percentage, fill = Likert)) + geom_col(position = position_likert(reverse = FALSE)) + geom_text( aes( label = label_percent_abs(hide_below = .01, accuracy = 1)(Percentage), color = after_scale(hex_bw(.data$fill)) ), position = position_likert(vjust = 0.5, reverse = FALSE), size = 3.5 ) + scale_y_continuous(labels = scales::percent) + labs( title = "Likert Responses by Category", x = "Category", y = "Percentage", fill = "Likert Scale" ) + coord_flip()+ theme_minimal()+scale_fill_manual(values = custom_colors) + labs(x = NULL, y = NULL, fill = NULL) # Horizontal bar plot bar_plot <- ggplot(df, aes(x = n, y = var)) + geom_bar(stat = "identity", fill = "lightgrey") + geom_label( aes( label = label_number_abs(hide_below = .05, accuracy = 2)(n) ), size = 3.5, position = position_stack(vjust = 0.5), hjust = 1, fill = NA, label.size = 0, color = "black" ) + scale_y_discrete(labels = \(x) gsub("\\..*$", "", x)) + scale_x_continuous( labels = label_percent_abs(), expand = c(0, .15) ) + theme_light() + theme( legend.position = "bottom", panel.grid.major.y = element_blank(), axis.text.x = element_blank() # Hides x-axis numbers ) + labs(x = NULL, y = NULL, fill = NULL) # Print plots (likert_plot) +(bar_plot)+ plot_layout( width = c(4,1) ) & theme(legend.position = "bottom") 看起来像这样: 我想要: 每个条形级别内的百分比,四舍五入至小数点后 0 位 让他们大胆 将右侧和左侧的总数相加。(即左侧的总数是非常不满意和满意的总和,右侧的总数是满意和非常满意的总和) 如何在 R 中实现这些? 数据 dput(df) structure(list(var = c("A", "B", "C", "D", "E"), n = c(106L, 106L, 87L, 102L, 99L), `Very Dissatisfied` = c(18.8679245283019, 19.1823899371069, 22.2222222222222, 18.9542483660131, 22.2222222222222 ), Dissatisfied = c(14.4654088050314, 16.0377358490566, 25.2873563218391, 18.9542483660131, 20.5387205387205), Neutral = c(22.9559748427673, 25.4716981132075, 15.7088122605364, 21.2418300653595, 20.8754208754209 ), Satisfied = c(22.0125786163522, 18.8679245283019, 17.2413793103448, 22.8758169934641, 17.5084175084175), `Very Satisfied` = c(21.6981132075472, 20.440251572327, 19.5402298850575, 17.9738562091503, 18.8552188552189 )), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame" )) 要获得所需的结果,请将 scale= 中的 label_percent_abs 参数设置为 1,以避免比例乘以默认的 100。要使用粗体标签,请使用 fontface="bold" 中的 geom_text。最后,要获取标签,您可以使用单独的数据框来计算总数(类似于我在回答您之前的问题时所做的操作),然后使用第二个geom_text/label: df <- data.frame( var = c("A", "B", "C", "D", "E"), n = c(106, 106, 87, 102, 99), `Very \n Dissatisfied` = c(18.9, 19.2, 22.2, 19.0, 22.2), Dissatisfied = c(14.5, 16.0, 25.3, 19.0, 20.5), Neutral = c(23.0, 25.5, 15.7, 21.2, 20.9), Satisfied = c(22.0, 18.9, 17.2, 22.9, 17.5), `Very \n Satisfied` = c(21.7, 20.4, 19.5, 18.0, 18.9), check.names = FALSE ) library(tidyverse) library(ggstats) library(patchwork) levels <- names(df)[-c(1:2)] df_long <- df %>% select(-n) %>% pivot_longer(!var, names_to = "Likert", values_to = "Percentage") |> mutate(Likert = factor(Likert, levels)) custom_colors <- c( "#D91828", "#D98989", "#71B1D9", "#71F26D", "#17A621" ) names(custom_colors) <- levels df_tot <- df_long |> summarise( prop_lower = sum(Percentage[Likert %in% levels[1:2]]), prop_higher = sum(Percentage[Likert %in% levels[4:5]]), .by = var ) |> pivot_longer(-var, names_prefix = "prop_", values_to = "Percentage", names_to = "where" ) # Likert plot likert_plot <- ggplot(df_long, aes(x = Percentage, y = var, fill = Likert)) + geom_col(position = position_likert(reverse = FALSE)) + geom_text( aes( label = label_percent_abs(hide_below = .01, accuracy = 1, scale = 1)(Percentage), color = after_scale(hex_bw(fill)) ), position = position_likert(vjust = 0.5, reverse = FALSE), size = 3.5, fontface = "bold" ) + geom_label( data = df_tot, aes( label = label_percent_abs(hide_below = .01, accuracy = 1, scale = 1)(Percentage), x = ifelse(where == "lower", -.6, .6), fill = NULL ), size = 3.5, fontface = "bold", label.size = 0, show.legend = FALSE ) + scale_x_continuous( labels = label_percent_abs() ) + labs( title = "Likert Responses by Category", x = "Category", y = "Percentage", fill = "Likert Scale" ) + theme_minimal() + scale_fill_manual(values = custom_colors) + labs(x = NULL, y = NULL, fill = NULL) + coord_cartesian(clip = "off") # Horizontal bar plot bar_plot <- ggplot(df, aes(x = n, y = var)) + geom_bar(stat = "identity", fill = "lightgrey") + geom_label( aes( label = label_number_abs(hide_below = .05, accuracy = 2)(n) ), size = 3.5, position = position_stack(vjust = 0.5), hjust = 1, fill = NA, label.size = 0, color = "black" ) + scale_y_discrete(labels = \(x) gsub("\\..*$", "", x)) + scale_x_continuous( labels = label_percent_abs(), expand = c(0, .15) ) + theme_light() + theme( legend.position = "bottom", panel.grid.major.y = element_blank(), axis.text.x = element_blank() # Hides x-axis numbers ) + labs(x = NULL, y = NULL, fill = NULL) # Print plots (likert_plot) + (bar_plot) + plot_layout( width = c(4, 1) ) & theme(legend.position = "bottom")
假设我有以下情节 库(ggplot2) 图书馆(动物园) dat = rbind(data.frame('季度' = as.yearqtr(as.Date(c('2000-01-01', '2002-01-01', '2004-01-01', '2006-01 -01'))), 'Val' = c(10,20,30,40), '
reorder_within() 神秘地没有在我的数据的方面内排序
我一定错过了一些简单的东西,但我一生都无法确定为什么我无法在我自己的数据上重现 tidytext::reorder_within() 的行为。 虹膜达一切正常...
当我使用下面的代码和数据制作多面系数图时,y 轴在每个类别行上重复。有没有一种简单的方法可以使只有相关的 y 轴标签/刻度线...
我有以下ggplot 库(ggplot2) 图书馆(动物园) dat = rbind(data.frame('季度' = as.yearqtr(as.Date(c('2000-01-01', '2002-01-01', '2004-01-01', '2006-01 -01'))), 'Val' = c(1,2,3,4), 类型 = r...
我试图覆盖样本中属于每个条形的人数百分比,但我无法将数字放在每个条形的顶部。即使我尝试简单地将每个百分比与 0 对齐,...
我正在尝试创建一个看起来像这样的图表,其中男性和女性的治疗平均值在 y 轴上水平镜像,但我正在努力弄清楚 ggplot 是如何实现的: 想要的情节 我...
我尝试使用 R ggplot 绘制数据,但时间标签无法正确显示在 x 轴上。 任何建议将不胜感激。 示例数据如下所示: > dput(头(df1,10))
闪亮:<-: replacement has length zero when using wrapping X Axis Label
我正在尝试创建一个闪亮的仪表板,用户可以在其中根据下拉列表选择一个县。我的 X 轴值是长字符串,我想将其包装起来以便正确渲染。当我不使用时
我正在使用 Rshiny 制作交互式绘图。目的是比较不同城市的空气质量数据。这应该在 ggplot 中完成,用户可以在其中选择污染物(y 轴)和
Worldphones 闪亮应用程序不显示 ggplot barplot
我正在尝试使用 worldphones 数据集构建一个闪亮的应用程序,但 ggplot 条形图未显示。 我使用两个单独的文件,即 ui.R 和 server.R 您可以在下面找到我的代码。可以请你吗
数据: 图书馆(土地) 图书馆(传奇) 图书馆(tidyverse) 图书馆(tidyterra) 米 <- matrix(c(1:24,25), nrow=5, ncol=5) n <- matrix(rep(5,time=25),nrow = 5,ncol = 5) ## as.double() ---- dt &l...
我正在为我的嵌套方差分析模型运行 TukeyHSD 事后测试(响应=IAUC,因子=基因型+线,其中线嵌套在基因型中)。 但是我在运行 Tukey lette 的脚本时遇到错误...
我对 qic 图 - 控制图有疑问。 我的 x.axis 没有绘制我想要的所有日期。我将日期四舍五入为每 14 天,周期为 59 周。我想要所有这些都被绘制出来,是的...
我有一个名为 df 的数据框,其中包含 Likert 数据和一个具有 5 个级别的列 var。 我想根据特定顺序对 Likert 和条形图进行排序。例如,我想对它们进行排序...