1
votes

I am trying to write a code that I wrote with a basic graphics package in R to ggplot.

The graph I obtained using the basic graphics package is as follows:

Example image containing six graphs

I was wondering whether this type of graph is possible to create in ggplot2. I think we could create this kind of graph by using panels but I was wondering is it possible to use faceting for this kind of plot. The major difficulty I encountered is that maximum and minimum have common lengths whereas the observed data is not continuous data and the interval is quite different.

Any thoughts on arranging the data for this type of plot would be very helpful. Thank you so much.

2
This is simple to produce in ggplot with different layers and facets. Post some sample data and your best attempt at producing a ggplot, and somebody can surely help.Andrie
I have one question. In the above figure, the legend is not showing properly. Maximum and minimum should not have point there. Is it possible to remove the points from maximum and minimum in above figure ? Thanks.Jd Baba

2 Answers

1
votes

Jdbaba,

From your comments, you mentioned that you'd like for the geom_point to have just the . in the legend. This is a feature that is yet to be implemented to be used directly in ggplot2 (if I am right). However, there's a fix/work-around that is given by @Aniko in this post. Its a bit tricky but brilliant! And it works great. Here's a version that I tried out. Hope it is what you expected.

# bind both your data.frames
df <- rbind(tempcal, tempobs)

p <- ggplot(data = df, aes(x = time, y = data, colour = group1, 
                 linetype = group1, shape = group1))
p <- p + geom_line() + geom_point()
p <- p + scale_shape_manual("", values=c(NA, NA, 19))
p <- p + scale_linetype_manual("", values=c(1,1,0))
p <- p + scale_colour_manual("", values=c("#F0E442", "#0072B2", "#D55E00"))
p <- p + facet_wrap(~ id, ncol = 1)
p

The idea is to first create a plot with all necessary attributes set in the aesthetics section, plot what you want and then change settings manually later using scale_._manual. You can unset lines by a 0 in scale_linetype_manual for example. Similarly you can unset points for lines using NA in scale_shape_manual. Here, the first two values are for group1=maximum and minimum and the last is for observed. So, we set NA to the first two for maximum and minimum and set 0 to linetype for observed.

And this is the plot:

enter image description here

0
votes

Solution found: Thanks to Arun and Andrie

Just in case somebody needs the solution of this sort of problem.

The code I used was as follows:

library(ggplot2)
tempcal <-  read.csv("temp data ggplot.csv",header=T, sep=",")
tempobs <- read.csv("temp data observed ggplot.csv",header=T, sep=",")
p <- ggplot(tempcal,aes(x=time,y=data))+geom_line(aes(x=time,y=data,color=group1))+geom_point(data=tempobs,aes(x=time,y=data,colour=group1))+facet_wrap(~id)
p

The dataset used were https://www.dropbox.com/s/95sdo0n3gvk71o7/temp%20data%20observed%20ggplot.csv

https://www.dropbox.com/s/4opftofvvsueh5c/temp%20data%20ggplot.csv

The plot obtained was as follows:

enter image description here

Jdbaba