Stacked Bar Graph reproduction in R

后端 未结 2 1955
花落未央
花落未央 2021-01-28 12:01

I am trying to reproduce this graph in R without success:

But for more years

This is the data:

title   2016 phased 2017 phased 2018 pha         


        
2条回答
  •  花落未央
    2021-01-28 12:50

    Read the data in the first instance including these arguments:

    read.xlsx(..., header = T, check.names = F)
    

    This will stop your headers being included in the long format data frame and also stop R appending the X's and .'s into your legend labels. Hopefully this will fix your y-axis tick marks by making all values numeric (it currently contains strings, making it character type).

    If this doesn't help, you could remove the titles from the dataframe into a legend_labs vector. You can the use this to add custom labels to the legend:

    legend_labs <- c("Pillar 1", "Pillar 2"...)
    ggplot(...)
    + scale_color_manual(labels = legend_labs)
    

    Then you could use this to label your x, y and legend titles:

    + labs(x = "X Title", y = "Y title", fill = "Legend Title")
    

提交回复
热议问题