I'm trying to use R for plotting a combo chart like the one in the image.
I tried to use the code displayed in the tutorial linked below, which used the line representation for the dot, but it gives me the errorr "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
(
You wouldn't need a line graph here since your location is categorical. So instead, you should use geom_point
instead of geom_line
and you do not have to use group = 1
anymore.
ggp <- ggplot(dataco)
geom_bar(aes(x=Location, y=B2014),stat="identity", fill="cyan",colour="#006000")
geom_point(aes(x=Location, y=BAU2050),stat="identity",color="red",size=2)
labs(title= "try",
x="commodities",y="%import")
scale_y_continuous(sec.axis=sec_axis(~.*1,name="Percentage"))
ggp
But since the scales of your B2014
and BAU2050
are the same, why don't you instead use just one y axis like the one below.
dataco |>
pivot_longer(cols = c("B2014", "BAU2050"),
names_to = "Source",
values_to="Percent") %>%
ggplot(aes(x = Location, y = Percent, fill = Source))
geom_bar(stat = "identity", position = "dodge")