I created a barchart with ggplot2 geom_bar and want to have two metrics in the bars. I used melt to do so. However, I now need a second y-axis with another scale, because the numbers of the two metrics are too different.
In the following the dataframe and the code I used:
df <- data.frame(categories = c("politics", "local", "economy", "cultural events",
"politics", "local", "economy", "cultural events"),
metric = c("page", "page", "page", "page",
"product", "product", "product", "product"),
value = c(100L, 50L, 20L, 19L,
950000L, 470000L, 50000L, 1320L))
In the following the code I used to create the plot and the second y-axis:
x <- ggplot(df, aes(x=categories, y=value, fill = metric))
x + geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(sec.axis=sec_axis(~. *1000), limits=c(1,1000))
However, now no bars appear in the chart anymore... Does anybody know how to solve this problem?