Reputation: 11
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:
Upvotes: 0
Views: 1823
Reputation: 1803
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:
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.
Upvotes: 1