R ggplot显示图表底部的数据表
我想在图表底部显示一个数据表,如下所示: 我一直在尝试post中的代码,但它没有在我的RStudio版本1.1.383中运行 图表的数据框为:R ggplot显示图表底部的数据表,r,ggplot2,R,Ggplot2,我想在图表底部显示一个数据表,如下所示: 我一直在尝试post中的代码,但它没有在我的RStudio版本1.1.383中运行 图表的数据框为: Pay.Quartiles = rep(c("lower", "lower.mid", "upper.mid", "upper"),2) Gender = rep (c("Female", "Male"), each = 4) Percentage = c(65.1,57.5,47.4, 41.3, 34.9, 42.5, 52.6, 58.7) df
Pay.Quartiles = rep(c("lower", "lower.mid", "upper.mid", "upper"),2)
Gender = rep (c("Female", "Male"), each = 4)
Percentage = c(65.1,57.5,47.4, 41.3, 34.9, 42.5, 52.6, 58.7)
df = data.frame (Pay.Quartiles, Gender, Percentage)
为了获得更好的标签而进行的一些数据争用,以及通过一些条带放置调整进行的刻面处理相结合,应该可以达到以下目的:
library(hrbrthemes) # gitlab.com/hrbrmstr/hrbrthemes or github
library(ggplot2)
Pay.Quartiles <- rep(c("Lower", "Lower Middle", "Upper Middle", "Upper"), 2)
Gender <- rep(c("Female", "Male"), each = 4)
Percentage <- c(65.1,57.5,47.4, 41.3, 34.9, 42.5, 52.6, 58.7)/100
xdf <- data.frame (Pay.Quartiles, Gender, Percentage, stringsAsFactors=FALSE)
xdf$lab <- sprintf("%s\n%s", Gender, scales::percent(Percentage))
ggplot(xdf) +
geom_col(
aes(lab, Percentage, fill = Gender),
width = 0.5, show.legend = FALSE
) +
facet_wrap(
~Pay.Quartiles, scales = "free_x", nrow = 1, strip.position = "bottom"
) +
hrbrthemes::scale_y_percent() +
hrbrthemes::scale_fill_ipsum() +
labs(x = NULL, y = NULL) +
hrbrthemes::theme_ipsum_rc(grid = "Y") +
theme(strip.placement = "outside") +
theme(strip.text = element_text(hjust=0.5)) +
theme(panel.spacing.x = unit(0.33, "lines"))
RStudio版本有些不相关,R&ggplot2的版本可能更重要。理想情况下,你应该在帖子中包含你尝试过的代码,这样想回答的人就可以使用你尝试过的代码了人类是不完美的,你可能已经错误地转录了代码,而不必访问另一个网站试图提供帮助,非常感谢。我不知道这个包裹。这太棒了!