Problem in errorbar of ggplot2

Here, is the code.
but i cannot add the error. maybe something wrong. Please, help me to fix the problem
library(readr)
data <- read_csv("data.csv")
View(data)
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()
    output
    Incase of adding errorbar:
    Error: Cannot use +.gg() with a single argument. Did you accidentally put + on a new line?

My data set is...

Month season POC sd_min sd_max
June Southwest Monsoon 103.780225 88.12418851 119.4362615
July Southwest Monsoon 161.7999176 139.8901679 183.7096674
Aug Southwest Monsoon 147.8307 126.0604078 169.6009923
Sep Southwest Monsoon 151.1334294 137.6584583 164.6084005
Oct Post Monsoon 155.3259882 145.1971328 165.4548436
Nov Post Monsoon 169.3289235 151.2990747 187.3587723
Dec Northeast Monsoon 181.0842882 170.5199668 191.6486096
Jan Northeast Monsoon 184.2212588 169.8248833 198.6176343
Feb Northeast Monsoon 180.4129706 171.8540214 188.9719198
March Pre Monsoon 161.1084529 144.4730513 177.7438545
April Pre Monsoon 133.1878471 116.1223398 150.2533544
May Pre Monsoon 115.3821118 94.72632631 136.0378973

Try this. Notice that the + signs are at the end of each line.

ggplot(data, aes(x = Month, y = POC, group = 1)) + geom_line() + geom_point() +    
  geom_errorbar(ymax = sd_max, ymin = 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()

In that case, showing the output is

ggplot(data, aes(x = Month, y = POC, group = 1)) + geom_line() + geom_point() +

  • geom_errorbar(ymax = sd_max, ymin = 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()
    

Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomErrorbar, :
object 'sd_max' not found

Oops, I did not notice you left the aes() out of the geom_errorbar. Notice that in my code I changed the name of the data frame to DF. There is a function called data, so I avoid using that as a variable name. The would not cause you this problem, I just avoid using that name.

library(ggplot2)
DF <- read.csv("c:/users/fjcc/Documents/R/Play/Dummy.csv", sep = "\t")

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

Created on 2020-05-02 by the reprex package (v0.3.0)

1 Like

Thanks, it works. it's amazing

Sorry for disturbing once again,
How to increase the resolution of the graph.
when I saved that, it was very low resolution. such as the axis data are not clear, and the line also.

Did you use the ggsave() function? That works well for me. I displayed the graph and then ran

ggsave("Test.png")
1 Like

Hello, sir,
How to remove the grid line of the figure? I think, if the grid line are removed , then it will be very nice.

With Thanks
Rony

Try adding replacing theme_bw() with theme_classic() at the end of the ggplot command.

1 Like

My last asking to you,
If I want to visualize the season included in the above data set just like the image.
Then what code is added to the plot.....
Please, help me to find the codeWhatsApp Image 2020-05-03 at 4.38.18 PM .

I tried but code shows wrong. It is the addition of a secondary layer in X-axis probably

This is the closest I could get. Other people may know a way to do better.

library(ggplot2)
DF <- read.csv("c:/users/fxcampos/Documents/R/Play/Dummy.csv", sep = "\t",
               stringsAsFactors = FALSE)
DF$Month <- factor(DF$Month, levels = c("June","July", "Aug", "Sep", "Oct", "Nov", "Dec",
                                        "Jan", "Feb", "March", "April", "May"), ordered = TRUE)
DF$Season <- rep(c("Summer", "Fall", "Winter", "Spring"), each = 3)
DF$Season <- factor(DF$Season, levels = c("Summer", "Fall", "Winter", "Spring"))
head(DF)
#>   Month           season      POC    sd_min   sd_max Season
#> 1  June SouthwestMonsoon 103.7802  88.12419 119.4363 Summer
#> 2  July SouthwestMonsoon 161.7999 139.89017 183.7097 Summer
#> 3   Aug SouthwestMonsoon 147.8307 126.06041 169.6010 Summer
#> 4   Sep SouthwestMonsoon 151.1334 137.65846 164.6084   Fall
#> 5   Oct      PostMonsoon 155.3260 145.19713 165.4548   Fall
#> 6   Nov      PostMonsoon 169.3289 151.29907 187.3588   Fall

ggplot(DF, aes(x = Month, y = POC, group = 1)) + geom_line() + geom_point() +    
  geom_errorbar(aes(ymax = sd_max, ymin = sd_min), width = 0.25) +    
  # coord_cartesian(ylim = c(0.95 * min(DF$sd_min), 1.05 * max(DF$sd_max)),
  #                 expand = FALSE, clip = "off")+
  facet_wrap( ~ Season, strip.position = "bottom", scales = "free_x", nrow = 1) +
  theme(panel.spacing.x = unit(0, "lines"),
        strip.background = element_blank(),
        strip.placement = "outside",
        panel.background = element_blank())

Created on 2020-05-08 by the reprex package (v0.3.0)

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