简体   繁体   English

ggplot2 条形图在使用 position = "dodge" 时中断

[英]ggplot2 barplot breaks when position = "dodge" used

I'm currently working on a COVID-19 Germany Shiny App for an University Project.我目前正在为大学项目开发​​ COVID-19 Germany Shiny 应用程序。 I'm trying to make a barplot that shows the daily infection numbers of different regional levels of Germany.我正在尝试制作一个条形图,显示德国不同区域级别的每日感染人数。 This is not a specific problem with Shiny App, it's more ggplot.这不是 Shiny App 的特定问题,它更像是 ggplot。 I reproduced the problem without the Shiny App enviornment.我在没有 Shiny App 环境的情况下重现了这个问题。 My basic Code is the following:我的基本代码如下:

require(tidyverse)
library(tidyverse)
require(lubridate)
library(lubridate)
library(readr)
require(zoo)
library(zoo)


data <- read_csv("https://opendata.arcgis.com/datasets/dd4580c810204019a7b8eb3e0b329dd6_0.csv")

## Data Coding data Datensatz
data$Meldedatum <- ymd_hms(data$Meldedatum)
data$Meldedatum <- date(data$Meldedatum)

# Label Deutschland

data$label_de <- paste("Deutschland")

# Label Deutschland - Alter

data$label_de_age <- paste(data$label_de, data$Altersgruppe)

# Label Bundesland Alter

data$label_bl_age <- paste(data$Bundesland, data$Altersgruppe)

# Label SK/LK Alter

data$label_sklk_age <- paste(data$Landkreis, data$Altersgruppe)

#Data Long
data_long <- data[c( "Meldedatum", "AnzahlFall","Bundesland", "Landkreis" ,"label_de_age", "label_bl_age", "label_sklk_age")]
data_long$Deutschland <- "Deutschland"

data_long<- pivot_longer(data_long, -c( Meldedatum, AnzahlFall), values_to = "Gebiet")

data_long<- data_long[c("Meldedatum", "AnzahlFall", "Gebiet")] 

The specific new labels for the column data_long$Gebiet are important for my shiny App.data_long$Gebiet的特定新标签对我闪亮的应用程序很重要。 Now if I want to plot the daily infection numbers of eg "Deutschland" (Germany) and "Bayern" (Bavaria) without position = "dodge" , my graph looks like the following, which is fine at first.现在,如果我想在没有position = "dodge"情况下绘制例如 "Deutschland" (Germany) 和 "Bayern" (Bavaria) 的每日感染数量,我的图表如下所示,一开始没问题。

# Plot Deutschland and Bayern
ggplot(data =  subset(data_long, Gebiet %in% c("Deutschland", "Bayern" )), 
       mapping = aes(
         x= Meldedatum,
         y= AnzahlFall,
         fill = Gebiet
       ) )+
  geom_bar(stat = "identity")

没有闪避的情节1

But if I now add the line position = "dodge" to geom_bar() my plot breaks.但是,如果我现在将 line position = "dodge"geom_bar()我的情节就会中断。 And looks like the following.看起来像下面这样。

# Plot Deutschland and Bayern with dodge
ggplot(data =  subset(data_long, Gebiet %in% c("Deutschland", "Bayern" )), 
       mapping = aes(
         x= Meldedatum,
         y= AnzahlFall,
         fill = Gebiet
       ) )+
  geom_bar(stat = "identity", position = "dodge")

Plot2 与闪避

Does somebody know why this happens and how to fix this?有人知道为什么会发生这种情况以及如何解决这个问题吗?

Thanks for the help.谢谢您的帮助。

The issue is that you have multiple observations per date.问题是您每个日期有多个观察结果。 Therefore you get multiple bars per date (and of course region) when using position="dodge" .因此,当使用position="dodge"时,每个日期(当然还有区域)会得到多个条形。 To solve this issue aggregate your data by date and region before plotting, eg by using count(Meldedatum, Gebiet, wt = AnzahlFall) which will add a new variable (named n by default) to your df with the sum of cases per date and region:要解决此问题,请在绘图前按日期和区域聚合您的数据,例如通过使用count(Meldedatum, Gebiet, wt = AnzahlFall)将一个新变量(默认命名为n )添加到您的 df 中,其中包含每个日期和地区:

library(tidyverse)
library(lubridate)
library(readr)
library(zoo)


data <- read_csv("https://opendata.arcgis.com/datasets/dd4580c810204019a7b8eb3e0b329dd6_0.csv")

## Data Coding data Datensatz
data$Meldedatum <- ymd_hms(data$Meldedatum)
data$Meldedatum <- date(data$Meldedatum)

# Label Deutschland

data$label_de <- paste("Deutschland")

# Label Deutschland - Alter

data$label_de_age <- paste(data$label_de, data$Altersgruppe)

# Label Bundesland Alter

data$label_bl_age <- paste(data$Bundesland, data$Altersgruppe)

# Label SK/LK Alter

data$label_sklk_age <- paste(data$Landkreis, data$Altersgruppe)

#Data Long
data_long <- data[c( "Meldedatum", "AnzahlFall","Bundesland", "Landkreis" ,"label_de_age", "label_bl_age", "label_sklk_age")]
data_long$Deutschland <- "Deutschland"

data_long<- pivot_longer(data_long, -c( Meldedatum, AnzahlFall), values_to = "Gebiet")

data_long<- data_long[c("Meldedatum", "AnzahlFall", "Gebiet")] 

data_long %>% 
  count(Meldedatum, Gebiet, wt = AnzahlFall) %>% 
  filter(Gebiet %in% c("Deutschland", "Bayern")) %>% 
  ggplot(mapping = aes(
         x= Meldedatum,
         y= n,
         fill = Gebiet
       ))+
  geom_bar(stat = "identity", position = "dodge")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM