Multiple graphs in ggplot2: Aesthetics must be either the same or the same length

I am using the following dataset (downloadable here ) and code (below) trying to plot multiple graphs in one ggplot. I know that there are many explanations there, but still I do not seem to be doing this work, because I am confused about where to put the commands for ggplot to understand what I want.

In addition, I know that there may be source data in two ways: either in wide or in long format. When I store data in a wide format , I have to write a lot to complete the task (see code and graph below), but when I convert it to a long format , ggplot complains about the absence of values ​​(see code and error message below).

This is my minimal code example:

library(ggplot2) # for professional graphs
library(reshape2) # to convert data to long format

WDI_GDP_annual <- WDI[which(WDI$Series.Name=='GDP growth (annual %)'),] # extract data I need from dataset
WDI_GDP_annual_short <- WDI_GDP_annual[c(-1,-2,-4)] # wide format
test_data_long <- melt(WDI_GDP_annual_short, id = "Time") # long format

# (only successful) graph with wide format data
ggplot(WDI_GDP_annual_short, aes(x = Time)) +
 geom_line(aes(y = Brazil..BRA., colour = "Brazil..BRA.", group=1)) +
 geom_line(aes(y = China..CHN., colour = "China..CHN.", group=1)) +
 theme(legend.title = element_blank())

# several graphs possibilities to plot data in long format and to have to write less (but all complain)
ggplot(data=test_data_long, aes(x = time, y = value, colour = variable)) +
 geom_line() +
 theme(legend.title = element_blank())

ggplot(data=test_data_long, aes(x = time, y = value, color = factor(variable))) +
 geom_line() +
 theme(legend.title = element_blank())

ggplot(test_data_long, aes(x = time, y = value, colour = variable, group = variable)) +       
 geom_line()

This is the (only) successful plot that I have received so far, but I do not want to write so much (since I want to have 6 more graphs in this ggplot):

enter image description here

I know that using its long format means a more elegant way of building animations, but I have ever used what I use (see above). I always get the following complaints:

: , , dataProblems:

- ?

+4
1

: ".." , class character ( factor, stringsAsFactors).

".." NA, na.strings = ".." read.xxx. , numeric. str , .

library(reshape2)
library(ggplot2)

df <- read.csv(file = "https://dl.dropboxusercontent.com/u/109495328/WDI_Data.csv",
               na.strings = "..")
str(df)

# compare with
# df <- read.csv(file = "https://dl.dropboxusercontent.com/u/109495328/WDI_Data.csv")
# str(df)


# melt relevant part of the data
df2 <- melt(subset(df,
                   subset = Series.Name == "GDP growth (annual %)",
                   select = -c(Time.Code, Series.Code)),
        id.vars = c("Series.Name", "Time"))

ggplot(df2, aes(x = Time, y = value, colour = variable, group = variable)) +       
  geom_line()

enter image description here

+4

All Articles