Hi all,
I hoped someone could teach me how to make a plot with the following dataframe:
group season 1 season 2 season 3 season 4
bananas 1 4 5 7
apples 6 10 8 2
pears 3 5 10 4
What I want to create is a bargraph with on the x-axis all the yields of season 1 for bananas, apples and pears, so three columns. I want to do the same for season 2, 3 and 4, thus ending with a bar graph with four clusters of 3 bars, totalling to 12 bars. On the y axis the amount should be stated, so that the yields of different fruits per season can be compared. I also want to give the bars of one species the same color and indicate this in a legend. I have tried solving this myself using the melt function (which I did not get to work), the tidyverse gather function (also could not get it to work) and I also tried to use barplot (but this also did not work). An example of code I used, and which I intuitively felt like would probably be the easiest of my three tryouts to fix:
However, I as a beginner in R just cannot seem to make it work...
I have searched and tried out many tutorials and examples without solving this, soI am really hoping someone here is willing to take a look and help me out.
It is best to do your reshaping outside of ggplot and then pipe that result into ggplot(). I saved the result in an intermediate data frame in the code below just to illustrate the result of gather().
Thank you so much! I had tried the gather function before, but I see now that I made it too complex. The only thing I encounter now is that I want to add in error bars; I put the sd values in a dataframe with the exact same layout and dimensions as the season data, but due to the layout of my ggplot I cannot seem to get the error bars in the plot. What I have so far is:
Ofcourse the 'Value' variable won't be recognized since I specified a different dataframe for the geom_errorbar, I am not sure however how to make these errorbars work otherwise with this way of using ggplot
Your sdall_tall data frame should have a column named group and one named Season. You can then join meanall_tall and sdall_tall with the inner_join function from dplyr.
NewDF <- inner_join(meanall_tall, sdall_tall, by = c("group", "Season"))
NewDF will now have a Value column and an sd column and you can use NewDF as the data argument from ggplot() and set the values of ymin and ymax in geom_errorbar().
A small suggestion, the new pivot_longer() function in tidy 1.0.0 has made things easier to remember.
Here is a line replacing gather with pivot_longer() to get DFtall