'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)
)
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
Solution 1:[1]
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")
Solution 2:[2]
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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lucca Nielsen |
Solution 2 | Quinten |