我有一个条形图,我想要排序的条形图和计算百分比的变化从一个条形图和显示在绘图上. 我使用了因子,try 了不同的方法和stackoverflow上的所有东西,但它给了我各种各样的错误.我设法按照 colored颜色 对图例进行了排序,但条形图的 colored颜色 没有相同的顺序.

下面是我的代码:

library(ggplot2)

data$Active_period  <- factor(data$Active_period,levels = c("study_year","last_year", "this_year"))
fct_rev(data$Active_period)

ggplot(data, aes(count, Name, fill = Active_period)) +
geom_col(width = 0.8, position = position_dodge2(width = 0.8, preserve = "single"))+
facet_grid(Time ~ site)+
scale_fill_manual(name = " ",values = c("study_year" ="#5d5c5c", "this_year" ="#4475aa", 
"last_year" = "#333239"),limits = c("study_year","last_year", "this_year"))+
theme(legend.position = "bottom", 
          axis.text = element_text(size = 11), 
          axis.title = element_text(size = 11, face = "bold"),
          strip.text.x = element_text(size = 12),
          strip.text.y = element_text(size = 12),
          plot.title = element_text(size = 14, face = "bold"),
          plot.subtitle = element_text(size = 13)))

以下是我的数据:

Name | Active_period| site |Time | count
A | Last_year| north| mornings |10
A | Last_year| south| mornings |20
A | Last_year| north| evenings|45
A | Last_year| south| evenings|35
A | this_year| north| mornings |80
A | this_year| south| mornings |60
A | this_year| north| evenings|95
A | this_year| south| evenings|120
A | study_year| north| mornings |100
A | study_year| south| mornings |400
A | study_year| north| evenings|220
A | study_year| south| evenings|32
B | Last_year| north| mornings |10
B | Last_year| south| mornings |45
B | Last_year| north| evenings|25
B | Last_year| south| evenings|20
B | this_year| north| mornings |300
B | this_year| south| mornings |250
B | this_year| north| evenings|140
B | this_year| south| evenings|20
B | study_year| north| mornings |10
B | study_year| south| mornings |20
B | study_year| north| evenings|10
B | study_year| south| evenings|20

推荐答案

Here is a solution.
First, I create a custom theme so that the problem code is simpler. This is not really part of the question and it can be defined before the answer.

theme_so_q77804382 <- function(){ 
  theme_grey(base_size = 10) %+replace%
    theme(
      legend.position = "bottom", 
      axis.text = element_text(size = 11), 
      axis.title = element_text(size = 11, face = "bold"),
      strip.text.x = element_text(size = 12),
      strip.text.y = element_text(size = 12, angle = -90),
      plot.title = element_text(size = 14, face = "bold"),
      plot.subtitle = element_text(size = 13)
    )
}

The plot

该图是按名称计数的水平条形图,因此建议的方法不是将y轴定义为名称轴,而是像往常一样定义x轴中的自变量,然后用coord_flip反转.

现在问题来了.

  • 第一,强制Active_period个因子;
  • 然后,计算按NamesiteTime分组的百分比变化;
  • 现在我们有了从基本级别study_year开始的更改,反转因子级别以获得所要求的曲线图.

然后把它标出来,用geom_text标出百分比.文本也必须分组,因为fill不是geom_text美学,所以使用group = Active_period.用hjust稍微调整一下,以便在变化为负数时可以看到负号.

suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
})

data$Active_period[data$Active_period == "Last_year"] <- "last_year"

data %>%
  mutate(Active_period = factor(Active_period,levels = c("study_year","last_year", "this_year"))) %>%
  group_by(Name, site, Time) %>%
  arrange(Active_period) %>%
  mutate(perc = scales::percent(c(0, diff(count))/dplyr::lag(count, default  = 1))) %>%
  ungroup() %>%
  mutate(Active_period = forcats::fct_rev(Active_period)) %>%
  ggplot(aes(Name, count, fill = Active_period)) +
  geom_col(
    width = 0.8, 
    position = position_dodge2(width = 0.8, preserve = "single")
  ) +
  geom_text(
    position = position_dodge2(width = 0.8, preserve = "single"),
    aes(label = perc, group = Active_period),
    hjust = -0.2
  ) +
  scale_fill_manual(
    name = " ", 
    values = c(study_year ="#5d5c5c", this_year = "#4475aa", last_year = "#333239"),
    limits = c("study_year","last_year", "this_year")
  ) +
  coord_flip() +
  facet_grid(Time ~ site) +
  theme_so_q77804382()

创建于2024-01-12年第reprex v2.0.2

R相关问答推荐

geom_raster不适用于x比例中超过2,15的值

使用对管道内单元格的引用生成新变量

使用R的序列覆盖

如何将dygraph调用到R Markdown作为一个shiny 的react 对象的参数?

将嵌套列表子集化为嵌套列表

Rplotly中的Sankey Diagram:意外连接&

如何读取CSV的特定列时,给定标题作为向量

如何删除仅在数据集顶部和底部包含零的行

如何在PackageStatus()中列出&q;不可用的包&q;?

R-按最接近午夜的时间进行筛选

如何在ggplot2中创建多个y轴(每个变量一个)

将具有坐标列表列的三角形转换为多个多边形

如何使用FormC使简单算术运算得到的数字是正确的?

根据r中另一个文本列中给定的范围对各列求和

使用列中的值来调用函数调用中应使用的其他列

当由base::限定时,`[.factor`引发NextMethod错误

使用ggplot2绘制具有边缘分布的坡度图

R:使用ApexCharge更改标签在饼图中的位置

基于R中的引用将向量值替换为数据框列的值

R/shiny APP:如何充分利用窗口?