简体   繁体   中英

Plot bar graph with 3 variables

Im sure this is fairly easy but im new to R. how do you plot a bar graph with the following data.

D_data <- data.frame(month=c(1,2,3,4),
                 A=c(22,55,66,88),
                 B=c(7,8,5,8),
                 c=c(1,2,9,10))

The bar graph as x axis as the month, y as the value of each category A, B, C. There should be 3 bars for each month.

Since you tagged this with ggplot2 , I assume you don't want a base R solution, but you can use pivot_longer() to tidy your data and then use ggplot() with another argument.

More importantly, you don't have enough values to make boxplots with month and letters. However, you can make a bar plot.

library(tidyverse)
D_data %>%
  pivot_longer(cols = -month) %>%
  ggplot(aes(x = name, y = value, fill = name)) +
  geom_boxplot()

在此处输入图像描述

D_data %>%
  pivot_longer(cols = -month) %>%
  ggplot(aes(x = month, y = value)) +
  geom_bar(stat = "identity", 
           position = "dodge",
           aes(fill = name))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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