I want to create a barplot using ggplot in R studio using two variables side by side. I tried following other people suggestions I found online, but I cant get it to work.
Here's the data I'm using:
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
So, what I'm trying to do is have days on the x-axis and side by side barplots of x and y (with x & y being colored) corresponding to the day number.
First thing i did was make a data frame :
df1 <- data.frame(x,y,day)
and then I tried:
ggplot(df1, aes(x = day, y = x,y)) + geom_bar(stat = "identity",color = x, width = 1, position="dodge")
But I just can't get it to work properly. Any suggestions as to how I'd achieve this?
geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use geom_col() instead.
As a variation of a bar chart, a side-by-side bar chart is similar to a stacked bar chart, except it puts bars side by side instead of stacking them. Both side-by-side bar and stacked bar charts add a second dimension based on a standard bar chart. But stacked bar charts tend to show the part-to-whole relation.
Side By Side Bar Graphs To obtain side by side bar graphs in ggplot2, we need a lot of parts on top of the ggplot() command. geom_bar(stat = “identity”, position = position_dodge(), alpha = 0.75) gives the side by side bar graphs. ylim(0, 800) gives limits on the y-axis values. The geom_text() line adds labels to the bar graphs.
The following code shows how to create the barplot with multiple variables using the geom_bar () function to create the bars and the ‘dodge’ argument to specify that the bars within each group should “dodge” each other and be displayed side by side. ggplot (df, aes(fill=food, y=sales, x=stadium)) + geom_bar (position='dodge', stat='identity')
Now, we can create two ggplots with the following R code: The data object ggp1 contains a density plot and the data object ggp2 contains a scatterplot. Note that we could store any type of graphic or plot in these data objects.
Next we use position = "dodge"within geom_col()to make the bars un-stack. By default they will be stacking due to the format of our data and when he used fill = Statwe told ggplot we want to group the data on that variable. ggplot(dat_long, aes(x = Batter, y = Value, fill = Stat)) + geom_col(position = "dodge")
You have the right idea, I think the melt()
function from the reshape2
package is what you're looking for.
library(ggplot2)
library(reshape2)
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x, y, day)
df2 <- melt(df1, id.vars='day')
head(df2)
ggplot(df2, aes(x=day, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge')
EDIT
I think the pivot_longer()
function from the tidyverse tidyr
package might now be the better way to handle these types of data manipulations. It gives quite a bit more control than melt()
and there's also a pivot_wider()
function as well to do the opposite.
library(ggplot2)
library(tidyr)
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x, y, day)
df2 <- tidyr::pivot_longer(df1, cols=c('x', 'y'), names_to='variable',
values_to="value")
head(df2)
ggplot(df2, aes(x=day, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge')
Or you could use facet_wrap
to produce two plots:
library("ggplot2")
library("reshape")
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x,y,day)
df2 <- reshape::melt(df1, id = c("day"))
ggplot(data = df2, aes(x = day, y = value, fill = variable)) + geom_bar(stat = "identity")+ facet_wrap(~ variable) + scale_x_continuous(breaks=seq(1,12,2))
If you want the bars with color according to the day use fill = day
:
ggplot(data = df2, aes(x = day, y = value, fill = day)) + geom_bar(stat = "identity") + facet_wrap(~ variable) + scale_x_continuous(breaks=seq(1,12,2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With