Home > Mobile >  How could I bar_plot this data.frame?
How could I bar_plot this data.frame?

Time:05-14

vertejumi_ceturksnos <- data.frame(
  Vertejumi = c("0", "1", "2", "3"),
  Pirmais   = c(Pirmaiss),
  Otrais    = c(Otraiss),
  Tresais   = c(Tresaiss),
  Ceturtais = c(Pirmaiss)
)

Produced df enter image description here

I want to make a bar plot out of these values, so x axis <- are compared with values("Pirmais","Otrais","Tresais","Ceturtais") <- corresponding to quarters of the year, and y axis <- have bars reaching to the amount in the table, and the amounts are corresponding to the 0:3 values in the first column

CodePudding user response:

Try this, if your data format is identical as the image and i understood the question, should work.

library(tidyr)
library(ggplot2)
    
   


vertejumi_ceturksnos %>%
    pivot_longer(cols = -Vertejumi) %>%
    ggplot(aes(x = factor(name, 
                          levels = c("Primais", 
                                     "Otrais", 
                                     "Tresais", 
                                     "Ceturtais")),
               y = value, 
               fill = Vertejumi)) 
    geom_bar(stat = "identity") 
    labs(x = "x label", y = "y label")

CodePudding user response:

Reshaping the data using melt looks like this:

library(tidyverse)
library(reshape)
vertejumi_ceturksnos %>%
  melt() %>%
  ggplot(aes(x = variable, y = value, fill = Vertejumi))  
  geom_bar(stat = "identity")

Output:

enter image description here

  • Related