0

I have data with different countries, different years and different values for several variables on each country-year. I am trying to plot several multiple line plots (the lines are these variables/year) for each country.

For some unknown reason, the plots I get in return are empty, when running the following command:

par(mfrow=c(5,5))
mysplits<-split(A,A$country)
for (ii in 1:length(mysplits)) {
  adf<-mysplits[[ii]]
  snames<- names(mysplits)[ii]
  p<-plot(1, 
       main=paste(snames),
       type = "n", xlim=c(1993,2015),
       ylab = "value", xlab="years", ylim=c(-50,50))
  lines(adf$year, adf$tone, col = "steelblue", lty=5)
  lines(adf$year, adf$articles, col = "pink", lty=2) 
  lines(adf$year, adf$num, col = "red", lty=3) 
  lines(adf$year, adf$gold, col = "green", lty=6)
  legend("bottomright", legend = c("EPE Public","Tone","Coverage","No of Events",
  "Conflictual"), col = c("black","steelblue","pink","red","green"),
     lty = c(1,5,2,3,6),
     cex = 1.5, seg.len=4)
     }

Here is the output of dput(head(A)):

structure(list(X = 1:6, country = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("AU", "BE", "BU", "DA", "EN", "EZ", "FI", 
"FR", "GM", "HR", "HU", "IT", "LG", "LH", "LO", "LU", "MT", "NL", 
"PL", "RO", "SI", "SP", "SW"), class = "factor"), year = 1994:1999, 
gold = c(-0.571428571, -2.4, 1.26, -0.2, -0.966666667, 0.316666667
), tone = c(324L, 239L, 251L, 72L, 61L, 159L), articles = c(3.571428571, 
4.5, 8.2, 4.6, 9.333333333, 6.166666667), num = c(7L, 4L, 
5L, 5L, 6L, 6L), yepe = 1995:2000, couepe = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("AU", "BE", "BU", "DA", "EN", 
"EZ", "FI", "FR", "GM", "HR", "HU", "IT", "LG", "LH", "LO", 
"LU", "MT", "NL", "PL", "RO", "SI", "SP", "SW"), class = "factor"), 
epepub = c(2.01, 1.87, 0.55, 0.63, 0.52, 0.82), epepriv = c(NA, 
NA, 2.63, 2.71, 2.59, 2.12), epeind = c(0.65, 0.66, 0.72, 
0.63, 0.57, 0.53)), .Names = c("X", "country", "year", "gold", 
"tone", "articles", "num", "yepe", "couepe", "epepub", "epepriv", 
"epeind"), row.names = c(NA, 6L), class = "data.frame")

Here is an image of my data:

Header data

8
  • 1
    Do you have some data we can test with? Commented Feb 18, 2016 at 13:47
  • Hi, welcome to StackOverflow. The easiest way to give some data is to use dput(head(a)) and edit it into your opening post. Before doing this, make sure that you can replicate your problem with head(a). Also, what do you mean with empty plots? Do the axes still show up or is everything white? Commented Feb 18, 2016 at 14:00
  • 1
    Try the same script but with par(mfrow=c(1,1)). If this does work and gives a plot, then the problem is that you are trying to get too many plots on a single display. With par(mfrow=c(5,5)) each plot does not have enough room to be displayed. At least that is what seems to happen when I try to run the script. Commented Feb 18, 2016 at 14:15
  • 1
    send the plots to a graphics device: pdf('plots.pdf'); par(mfrow=c(5,5)); for (...); dev.off() Commented Feb 18, 2016 at 14:41
  • 1
    I converted your answer to an edit. Please don't use answers to add information to your question. Instead, edit the question. Also, I fixed your image. But please note that you should past the actual data in your question, not an image of the data. Images are much less useful! Commented Feb 18, 2016 at 18:27

1 Answer 1

1

I highly, highly, highly recommend getting away from the base plotting package when trying to do complex plotting in R. Your problem is, at Laterow pointed out, with the par(mfrow=c(5,5)) call. Instead of trying to debug what is going wrong there. Please consider using ggplot, which is a much more robust, readable and flexible library for doing r plotting. In the words of Hadley Wickham, ~"base R plotting is for creating drawings, ggplot is for understanding data"

For example, I believe the graph you are trying to produce can be accomplished with the following. Note, there is no for loop. In general, in R if you are using a for loop you are doing something wrong. Also note, its about half the lines of code and much more readable than what you originally put together.

library(ggplot2)
ggplot(A, aes(x=year)) + 
  geom_line(aes(y=tone, colour="Tone")) + 
  geom_line(aes(y=articles, colour="Coverage")) + 
  geom_line(aes(y=num, colour="No of Events")) +
  geom_line(aes(y=gold, colour="Conflictual")) + 
  facet_wrap(~country) + 
  scale_colour_manual("Legend", 
                      breaks = c("Tone", "Coverage", "No of Events", "Conflictual"),
                      values = c("steelblue", "pink", "red", "green")) +
  xlab("value") + 
  ylab("years")

Produces:

Graph

Note your sample data only had values for the "AU" country. However, this will automatically wrap you facets no matter how many countries are in this field. This is much more robust than having to statically say I have 25 countries which appears to be what you were doing in the original attempt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.