简体   繁体   中英

How could I bar_plot this data.frame?

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

Produced df 在此处输入图像描述

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

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")

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:

在此处输入图像描述

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