1

I have a dataframe that looks like this:

var1 var2 var3 year value
AT   EA   1    02   ...
AT   EA   1    03   ...
AT   XY   2    02   ...
AT   XY   2    03   ...
BE   EA   1    02   ...
BE   EA   1    03   ...
BE   XY   2    02   ...
BE   XY   2    03   ...

Value is just a numeric variable. What I want to do is produce a graphic which shows value in the y axis and year in the x axis. Furthermore, I want:

  • one plot for each level of var1 (that would be the factor condition in xyplot)

  • each level of var2 should have a different color

  • each level of var3 should be a different type of line graph (continuous vs. dashed)

Also, can I tell R to just use certain levels of var1 and var2 to make to graph? E.g. only use those observations where var1 is "AT" or "FR", but not those where it is "BE" or "DE"? Var1 and var2 both have 13 levels in the data.frame.

Edit: The data.frame can be downloaded here. I tried the following:

library(lattice)
xyplot(value~year | factor(report_ctry), data=EA17_flows_ex, groups=factor(indicator), type="l")
Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Laubsauger
  • 69
  • 2
  • 9
  • Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Apr 11 '17 at 08:20
  • 1
    I've edited the open post to include the data.frame and the code I've tried. – Laubsauger Apr 11 '17 at 13:33

2 Answers2

0

Perhaps something along the lines of? Code is untested. You should provide a small example, along with what you've tried and doesn't work, for best result.

library(ggplot2)

# will display only those in the list, AT and FR in this case
subxy <- xy[xy$var %in% c("AT", "FR"), ] 

ggplot(subxy, aes(x = year, y = value, color = var2, group = as.factor(var3))) +
  theme_bw() +
  geom_point(position = "jitter", width = 0.25) +
  facet_wrap(~ var1)
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
0

Hi I was not sure how you'd want var3 as a linetype. However, as different sizes it worked. To have only certain levels for var1 and var2 just subset your dataframe within ggplot or separately (e.g subset(dataframe,var1 %in% [list with var1-values you want to look at] & var2 %in% [list with var2-values you want to look at]))

library("ggplot2")

plot = ggplot(dataframe,aes(year,value))+
       geom_point(aes(colour=var2,size=var3))+facet_wrap(~var1)

ggsave("plot.png",plot,width=200,height=200,unit="mm")

enter image description here

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
Beveline
  • 267
  • 3
  • 9
  • I'd like to have a line graph that shows variable value dependent on var1, var2, and var3. I've added a link with the data.frame and the code I tried in the opening post. If I use "linetype" instead of "size", your solution works. Thank you. :) – Laubsauger Apr 11 '17 at 13:32