0

I am trying to make a line chart but failed. I am not generating the code for this. we have the mean and std error. Means of the month need to be grouped with a different color. I generated the graph in excel but it would be better for me to generate in R. My data are enter image description here enter image description here

I want to look such kind of image. It was made in excel. Please, help me to fix the problem

Rony Golder
  • 111
  • 6
  • 1
    What have you tried so far? Please edit the question to include your code and enough data to reproduce the graph (use `dput(my_data)`) to make the data easy to import – Richard Telford May 02 '20 at 07:23
  • 1
    The sooner you learn about ggplot2, the better. Check out https://ggplot2.tidyverse.org/reference/geom_linerange.html – Roman Luštrik May 02 '20 at 07:45

1 Answers1

0

You can start with the following code.

Prepare your data as a dataframe, like the one shown below. You can use read.csv to read data from a CSV file.

> data
       Month            season POC   sd_min   sd_max
1       June       Pre Monsoon 125 114.0564 135.9436
2       July Southwest Monsoon 145 137.9181 152.0819
3     August Northeast Monsoon 155 151.0996 158.9004
4  September Northeast Monsoon 155 145.0982 164.9018
5    October Northeast Monsoon 156 152.6262 159.3738
6   November  SouthwestMonsoon 152 134.6723 169.3277
7   December  SouthwestMonsoon 100  98.3175 101.6825
8    January       Pre Monsoon 150 143.3171 156.6829
9   February       Pre Monsoon 110 107.9925 112.0075
10     March       Pre Monsoon 153 148.8530 157.1470
11     April       Pre Monsoon 148 144.7181 151.2819
12       May  SouthwestMonsoon 146 141.7141 150.2859

NOTE: data$Month should be a factor with the levels ordered according to the order desired in the x-axis of the plot.

data$Month <- factor(data$Month, levels = data$Month)
library(ggplot2)

ggplot(data, aes(x = Month, y = POC, group = 1)) + geom_line() + geom_point()
 + geom_errorbar(ymax = data$sd_max, ymin = data$sd_min, width = 0.25)
 + coord_cartesian(ylim = c(0.95 * min(data$sd_min), 1.05 * max(data$sd_max)),
   expand = FALSE, clip = "off") + theme_bw()

Fig-1

To have a second layer of x-axis labeling you can refer here and here

  • How to calculate sd_max and sd_min. Truly speaking, I get only a single sd from a mean in excel. However, thanks for the comment – Rony Golder May 02 '20 at 09:03
  • Like this, `sd_min = mean - sd/2` and `sd_max = mean - sd/2`. – Ramasamy Kandasamy May 02 '20 at 09:10
  • The problem arises in the adding of the error bar such as Error: Cannot use `+.gg()` with a single argument. Did you accidentally put + on a new line? After generating the line the above problem arise. indicating "+geom_errorbar " this line show wrong. – Rony Golder May 02 '20 at 16:20
  • Sorry, they are different: `sd_min = mean - sd/2` and `sd_max = mean + sd/2`. I split the line with ggplot for readability. They should be in the same line. Sorry about that. – Ramasamy Kandasamy May 04 '20 at 07:48
  • Boss, the second layer of the X-axis in a line plot shows wrong. One is done but maintaining the same quartile. But in my case, Southwest monsoon=4 Months, Northeast monsoon=3 months, pre monsoon=3 months and post monssoon= 2 months, Then what can I do? Please, provide a dummy example as above the first figure you done. – Rony Golder May 09 '20 at 07:20
  • Hi, sorry, I am not familiar with how to make a second layer of x-axis. You can do it manually. You could export the image as an SVG file and use Inscape to edit it. – Ramasamy Kandasamy May 10 '20 at 09:49