1

I've been fumbling with this for a while now but let me start with the beginning.

This is how the first few lines of my data look like:

Year Action_mean_global Adventure_mean_global Fighting_mean_global Misc_mean_global Platform_mean_global
1 1980          0.3400000                   NaN                 0.77           0.6775                  NaN
2 1981          0.5936000                   NaN                  NaN              NaN               2.3100
3 1982          0.3622222                   NaN                  NaN           0.8700               1.0060
4 1983          0.4085714                   0.4                  NaN           2.1400               1.3860
5 1984          1.8500000                   NaN                  NaN           1.4500               0.6900
6 1985          1.7600000                   NaN                 1.05              NaN              10.7925
  Puzzle_mean_global Racing_mean_global Roleplaying_mean_global Shooter_mean_global Simulation_mean_global
1                NaN                NaN                     NaN             3.53500                    NaN
2           1.120000           0.480000                     NaN             1.00400                   0.45
3           3.343333           0.785000                     NaN             0.75800                    NaN
4           0.780000                NaN                     NaN             0.48000                    NaN
5           1.046667           1.983333                     NaN            10.36667                    NaN
6           0.802500                NaN                     NaN             1.00000                   0.03
  Sports_mean_global Strategy_mean_global Total_mean_global
1             0.4900                  NaN         1.2644444
2             0.1975                  NaN         0.7776087
3             0.5250                  NaN         0.8016667
4             3.2000                  NaN         0.9876471
5             3.0900                  NaN         3.5971429
6             1.9600                  NaN         3.8528571

They are all numeric.

Now, I simply wanted to do a plot with ggplot() + geom_line() to visualize change over year per genre. It works when doing it step by step, i.e.:

ggplot(df)+
  geom_line(aes_string(x = 'Year', y = plot_vector[1]))
  geom_line(aes_string(x = 'Year', y = plot_vector[2]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[3]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[4]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[5]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[6]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[7]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[8]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[9]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[10]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[11]))+
  geom_line(aes_string(x = 'Year', y = plot_vector[12]))

(plot_vector simply contains all column-names except for Year)

However, doing it in a for-loop:

p1 <- ggplot(df)+
  geom_line(aes_string(x = 'Year', y = plot_vector[1]))

for (plotnumber in 2:length(plot_vector))
{
  p1 <- p1 + 
    geom_line(aes_string(x = 'Year', y = plot_vector[plotnumber]))

}

I get the error message. Anyone can muster an idea?

Uwe
  • 41,420
  • 11
  • 90
  • 134

1 Answers1

0

Adding lines with a for loop to a ggplot object may have caused the reported error message but has a general problem caused by lazy evaluation. This has been asked frequently on SO, see, e.g., “for” loop only adds the final ggplot layer, or ggplot loop adding curves fails, but works one at a time.

However, ggplot2 works best when data are supplied in long format. Here, melt()from the data.table package is used to reshape df:

library(data.table)
molten <- melt(setDT(df), id.vars = c("Year"))

library(ggplot2)
ggplot(molten, aes(x = Year, y = value, group = variable, colour = variable)) +
  geom_line()

This creates the following chart:

enter image description here

Data

df <- structure(list(Year = 1980:1985, Action_mean_global = c(0.34, 
0.5936, 0.3622222, 0.4085714, 1.85, 1.76), Adventure_mean_global = c(NaN, 
NaN, NaN, 0.4, NaN, NaN), Fighting_mean_global = c(0.77, NaN, 
NaN, NaN, NaN, 1.05), Misc_mean_global = c(0.6775, NaN, 0.87, 
2.14, 1.45, NaN), Platform_mean_global = c(NaN, 2.31, 1.006, 
1.386, 0.69, 10.7925), Puzzle_mean_global = c(NaN, 1.12, 3.343333, 
0.78, 1.046667, 0.8025), Racing_mean_global = c(NaN, 0.48, 0.785, 
NaN, 1.983333, NaN), Roleplaying_mean_global = c(NaN, NaN, NaN, 
NaN, NaN, NaN), Shooter_mean_global = c(3.535, 1.004, 0.758, 
0.48, 10.36667, 1), Simulation_mean_global = c(NaN, 0.45, NaN, 
NaN, NaN, 0.03), Sports_mean_global = c(0.49, 0.1975, 0.525, 
3.2, 3.09, 1.96), Strategy_mean_global = c(NaN, NaN, NaN, NaN, 
NaN, NaN), Total_mean_global = c(1.2644444, 0.7776087, 0.8016667, 
0.9876471, 3.5971429, 3.8528571)), .Names = c("Year", "Action_mean_global", 
"Adventure_mean_global", "Fighting_mean_global", "Misc_mean_global", 
"Platform_mean_global", "Puzzle_mean_global", "Racing_mean_global", 
"Roleplaying_mean_global", "Shooter_mean_global", "Simulation_mean_global", 
"Sports_mean_global", "Strategy_mean_global", "Total_mean_global"
), class = "data.frame", row.names = c(NA, -6L))
Community
  • 1
  • 1
Uwe
  • 41,420
  • 11
  • 90
  • 134