One solution is to use the ggplot2 package
ggplot2
is part of the tidyverse
family of packages, as are tidyr
and dplyr
which I also use in the example below.
The %>%
(pipe) operator is imported from dplyr
, and passes the output of one function into another function's first argument. In a nutshell, x %>% f(y)
is equivalent to f(x,y)
.
I can't guarantee this will work without a reproducible example, but I'll talk you through it so you get the steps.
require(ggplot2)
require(dplyr)
require(tidyr)
### Format the data ------------------------------------------------------
formattedData <-
myData %>%
select(product_title, max_rating, min_rating) %>% #select only the columns we need
## Pivot longer takes us from this:
# |product_name | min_rating | max_rating|
# |"foo" | 1 | 325 |
# to this:
# |product_name | name | value |
# |"foo" |"min_rating"| 1 |
# |"foo" |"max_rating"| 325 |
# That's the data format ggplot() needs to do its stuff
pivot_longer(cols = all_of(c("max_rating", "min_rating")))
### Plot the data -------------------------------------------------------
ggplot(formattedData, # The data is our new 'formattedData' object
# aesthetics - X axis is product_title, y axis is value, # bar colour is name
aes(x = product_title, y = value, fill = name)) +
geom_bar(stat = "identity", position = "dodge") + # using the values, rather than counting elements
scale_fill_manual(values = c("max_rating" = "orange", "min_rating" = "blue") + # custom colours
ggtitle("Top products ratings") +
ylab("Ratings")