Home > front end >  Question when drawing a graph in r(dodge or geom_line)
Question when drawing a graph in r(dodge or geom_line)

Time:05-06

I want to express the frequency of each column (a,b,c,d,e) in a graph of (t) for each group. I don't know what to do. As shown in the figure, the data was selected in the form of a data frame, but I don't know what to do when I express it in the next graph. I tried to extract only the value for a with ggplot, but even this didn't work out. Is there a good way? Is there a way to express it with lines other than bar graphs? Help me. enter image description here

CodePudding user response:

Pivot the data longer to merge all counts of columns a to e inside just one name and value column, which can be used as dimension aesthetics for ggplot:

library(tidyverse)

data <- tribble(
  ~t, ~a, ~b,
  "m", 300, 100,
  "i", 20, 44,
  "j", 15, 99
)

data %>%
  pivot_longer(-t) %>%
  ggplot(aes(t, value, fill = name))  
  geom_col(position = "dodge")

Created on 2022-05-06 by the enter image description here

  • Related