Making plot with subsetting for year and months.

Hello everyone,
I have a data similar to this one:

tibble(year = c(rep("2012", 12), rep("2013", 12), rep("2014", 12), rep("2015", 12)),
month = c("Jan", "Feb", "Mar", "Apr", "Mag", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"Jan", "Feb", "Mar", "Apr", "Mag", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"Jan", "Feb", "Mar", "Apr", "Mag", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"Jan", "Feb", "Mar", "Apr", "Mag", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
species = c(rep("A", 20), rep("B", 18), rep("C", 5), rep("D", 5)),
color = c("blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green"),
value = runif(48)*100)

I would like to make a plot similar to the picture that I attach:
where the bars correspond to proportion of species, and the blue line correspond to the proportion of "blue" in color variable.

Thank you for the help.

I think you havent expressed your requirements effectively.
if your data is per month, as example provided, there is no month for which a blue value competes with any other, its either present or absent, so what sense does it make to talk of proportions ?

1 Like

You are right...
Should I post an example of my data even if is a large table?
Anyway, I have a variable with "year", another with "month", another with "species" , another with "frequency count of species", another with "ecological role".

I don't know if is better understandable the nature of the data that I'm working on.
Let me know, and thank you for your help.

Excuse me for the mistake. I have add a variable similar to the one I want to represent with the line.

df <-tibble(year = c(rep("2012", 12), rep("2013", 12), rep("2014", 12), rep("2015", 12)),
month = c("Jan", "Feb", "Mar", "Apr", "Mag", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"Jan", "Feb", "Mar", "Apr", "Mag", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"Jan", "Feb", "Mar", "Apr", "Mag", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"Jan", "Feb", "Mar", "Apr", "Mag", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
species = c(rep("A", 20), rep("B", 18), rep("C", 5), rep("D", 5)),
color = c("blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green", "yellow", "purple", "blue", "red", "green"),
value = runif(48)*100,
value_2 = runif(48)*100)

When i try to represent the line with geom_line() it doesn't work...
I would like to unite the dots with a line, but I don't know how I can do that.

df %>%
ggplot() +
geom_bar(aes(x = month, y = value), stat = "identity") +
facet_wrap(~ year) +
geom_point(aes(x = month, y = value_2))

Thank you for the help and for the patience.

Does adding a group aesthetic do it?

+ geom_line(aes(x = month, y = value_2, group = 1))

1 Like

Yes, thank you so much.

I notice that I have a problem.
The month on the x axes are sorted by alphabetical order.
How can I sort them from January to December?

mutate(month = factor(month, month.abb))

1 Like

I don't know why, but it do not work with my data.
Just the month "May" is in the new variable, and the rest of the months are replaced with NA.

Your example doesn't have a monyh May, it says Mag

found the solution:

mutate(month = factor(month, month.name))

I have to use month.name instead of month.abb because in my data I have month names in extended version.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.