ggplot2 相关问题

ggplot2是一个积极维护的R开源图表绘图包,由Hadley Wickham编写,基于“图形语法”的原则。它部分取代了R的基本图和格子包,同时提供了一个干净,强大,正交和有趣的API。

如何更改 stat_regline_equation() 中 R 平方的有效数字?

这是我的数据示例: df <- data.frame(surface_area = c(0.25, 1, 2.25, 4, 6.25), weight = c(0.8, 3.267, 7.733, 13.533, 21.8333)) The code for my plot and my plot: plot &...

回答 2 投票 0

如何在`coord_radial()`中提取坐标原点

我想在ggplot2(draw_ggcircle_list)中对齐两个圆形图,但我无法提取coord_radial()的坐标原点(0, 0)。现在下面的两个图不会按 x 轴对齐(所以...

回答 1 投票 0

R 和 ggplot 中从右到左的语言

我正在尝试让阿拉伯文本在 Mac 上的 R 中正确显示。目前,当我用阿拉伯语绘制绘图时,我必须切换到 Windows。 Windows 在 R 中正确显示阿拉伯语。但是在 Mac 中,我...

回答 1 投票 0

使用 R 中的 ggplot2 水平匹配两个图的水平

我在 R 中有一个名为 df 的数据框,其中包含 3 个问题的 Likert 数据和一个名为 var 的分组变量: var_levels <- c(LETTERS[1:5]) n = 500 likert_levels = c( "Very \n

回答 1 投票 0

如何根据多个条件增加 y 轴高度以适合 geom_text 标签?

我有一个应用程序,当未选择 p 值时,它看起来像这样: 当选择 p 值时,y 轴会扩展,这样星号就不会被截断: 其代码行是: {如果(...

回答 1 投票 0

如何减少 R 中 geom_dotplot 中 y 轴上的点之间的间距?

我正在尝试制作抗体滴度(y 轴)按位点(x 轴)范围从 1:20 到 1:640 的点图,但我无法减少 y 上点之间的空间轴。 我尝试将 log2 应用于 sc...

回答 1 投票 0

如何在plotnine中将x轴移动到绘图顶部?

我正在使用带有日期x轴图的plotnine,并希望将x轴日期值放在图表顶部,但找不到方法。 我在 ggplot 中看到它可以使用scale_x_discrete(posi...

回答 1 投票 0

R Shiny - 警告:[.data.frame 中的错误:创建两个交互式绘图时选择了未定义的列

我正在 R Shiny 中创建两个交互式图,虽然我可以让一个图显示并工作,但第二个图不断给我“警告:[.data.frame 中的错误:选择了未定义的列...”

回答 2 投票 0

为 Q-Q 图设置 x 轴和 y 轴的限制

我有下面的 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...

回答 1 投票 0

ggplot2 `geom_label()`中的警告消息

我在 R 中有一个数据框,经过一些数据转换、计算和绘图后: 图书馆(tidyverse) 库(ggstats) 图书馆(拼凑而成) 图书馆(小标题) 图书馆(tidyverse) 库(ggplot2)

回答 1 投票 0

“ggplot2”上的“position_dodge”和“geom_text_repel”不兼容

我在躲避图表上的 geom_text_repel 标签时面临问题。我在论坛上查看了其他类似的问题,但我无法准确理解如何强制回避这些标签。 ...

回答 1 投票 0

对 R 中 ggplot 2 中每个面网格上的 y 轴进行排序

我在 R 中有一个名为 X 的数据框。 X # 小标题:27 × 6 年份增量计数颜色标签类别 1 2024 -0....

回答 1 投票 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)...

回答 1 投票 0

在 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")

回答 1 投票 0

创建具有下限和上限的条形图

假设我有以下情节 库(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), '

回答 1 投票 0

reorder_within() 神秘地没有在我的数据的方面内排序

我一定错过了一些简单的东西,但我一生都无法确定为什么我无法在我自己的数据上重现 tidytext::reorder_within() 的行为。 虹膜达一切正常...

回答 1 投票 0

删除ggplot中facet_grid上重复的y轴标签

当我使用下面的代码和数据制作多面系数图时,y 轴在每个类别行上重复。有没有一种简单的方法可以使只有相关的 y 轴标签/刻度线...

回答 1 投票 0

固定时间序列条形图的宽度

我有以下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...

回答 1 投票 0

stat_bin 位置的奇怪行为

我试图覆盖样本中属于每个条形的人数百分比,但我无法将数字放在每个条形的顶部。即使我尝试简单地将每个百分比与 0 对齐,...

回答 1 投票 0

如何使用ggplot在条形图中水平镜像y轴上的平均值?

我正在尝试创建一个看起来像这样的图表,其中男性和女性的治疗平均值在 y 轴上水平镜像,但我正在努力弄清楚 ggplot 是如何实现的: 想要的情节 我...

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.