5r9n
5r9n

Reputation: 187

ggplot plots error bars but not actual data points in R

I have a dataframe read in from a .csv which looks like this:

valley_cw_summary:

"","times","means","sd"
"1",1,23.7326530612245,0.822951942679513
"2",10,NA,NA
"3",11,27.9811602527283,2.18451736644603
"4",12,NA,NA
"5",13,28.8594485927628,2.47839597165728
"6",14,NA,NA
"7",15,28.5562894887995,2.4613545973872
"8",16,NA,NA
"9",17,26.9750287026406,1.87035639782657
"10",18,NA,NA
"11",19,25.2288340034463,1.0835585618286
"12",2,NA,NA
"13",20,NA,NA
"14",21,24.5269385410684,0.804365453635496
"15",22,NA,NA
"16",23,24.1512923607122,0.806920352501217
"17",24,NA,NA
"18",25,24.0809803921569,0.826911680243558
"19",3,23.5923254472014,0.889646609799541
"20",4,NA,NA
"21",5,23.3741488747836,0.932515616519176
"22",6,NA,NA
"23",7,23.2863296955773,0.982225553711973
"24",8,NA,NA
"25",9,25.4694252873563,1.33025859840695

I attempt to plot this with the following script:

ggplot(data=valley_c_w_summary,aes(x = times,y=means))+
  theme_classic()+
  geom_line(data = valley_c_w_summary,aes(x=times,y=means))+
  geom_errorbar(data=valley_c_w_summary,aes(ymin=means-sd,ymax=means+sd))+
  labs(x="Time",y="Temperature in canopy May to December")

This plots only error bars (centered around the appropriate points as far as I can tell). I am plotting it with other such dataframes on the same plot, and they work fine, but they don't have any "NA"s, which leads me to believe they are the culprit. The full script of which looks like:

ggplot(data=ridge_cw_summary,aes(x = times,y=means))+
  geom_errorbar(data=ridge_c_w_summary,aes(ymin=means-sd,ymax=means+sd),colour="red")+
  geom_line(aes(y=means),colour="red")+
  theme_classic()+
  geom_line(data = valley_c_w_summary,aes(x=times,y=means))+
  geom_errorbar(data=valley_c_w_summary,aes(ymin=means-sd,ymax=means+sd))+
  geom_line(data = edge_c_w_summary,aes(x=times,y=means),colour="blue")+
  geom_errorbar(data=edge_c_w_summary,aes(ymin=means-sd,ymax=means+sd),colour="blue")+
  labs(x="Time",y="Temperature in canopy May to December")

How can I get ggplot to display the proper points?

Upvotes: 2

Views: 229

Answers (1)

PKumar
PKumar

Reputation: 11128

Alistaire comment sums up your answer, you need to put na.omit around your data frame also you don't need to call your data on each of the geoms, like below, I have copied your data and put it into a data frame called vally_c_w_summary:

ggplot(data=na.omit(valley_c_w_summary),aes(x = times,y=means))+
  geom_errorbar(aes(ymin=means-sd,ymax=means+sd),colour="red")+
  geom_line(aes(y=means),colour="blue",size=1)+
  theme_classic()+
  labs(x="Time",y="Temperature in canopy May to December")

I got the below graph, I hope this is what you are expecting:

enter image description here

In case however, you need to approximate the NAs , you can use a function called na.approx in zoo library.

Your code would be something like below:

library(zoo)
library(ggplot2)
ggplot(data=data.frame(na.approx(valley_c_w_summary)),aes(x = times,y=means))+
  geom_errorbar(aes(ymin=means-sd,ymax=means+sd),colour="red")+
  geom_line(aes(y=means),colour="blue",size=1)+
  theme_classic()+
  labs(x="Time",y="Temperature in canopy May to December")

The output would be little different now, the error bars have increased. You can read about the documenation of spline and linear transformations of the NA approximation using na.approx here

enter image description here

Upvotes: 1

Related Questions