Reputation: 87
This is my data frame:
library(ggplot2)
Year <- c(2019,2018,2017,2016,2015)
googlerev <- c(161857,136819,110855,90272,74989)
ibmrev <- c(77147,79591,79139,79919,81741)
df <- data.frame(Year, googlerev, ibmrev)
df
#> Year googlerev ibmrev
#> 1 2019 161857 77147
#> 2 2018 136819 79591
#> 3 2017 110855 79139
#> 4 2016 90272 79919
#> 5 2015 74989 81741
How can I make a plot as shown in the picture below:
(Revenue represents the value of google rev and ibmrev).
Upvotes: 3
Views: 68
Reputation: 10761
We can use pivot_longer
from the tidyr
package to change your data, then use geom_col
:
library(tidyr)
df %>%
pivot_longer(cols = -Year, names_to = "company", values_to = "revenue") %>%
ggplot(aes(Year, revenue, fill = company))+
geom_col(position = 'dodge')
Upvotes: 3
Reputation: 6226
This will be easier with long formatted data to control ggplot behavior. You can use reshape2
package for that.
library(ggplot2)
df <- reshape2::melt(df, id.vars = "Year")
ggplot(df, aes(x = Year, y = value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Year", y = "Revenue")
Upvotes: 1