Loops ggplot2+;循环结果为;错误:离散值提供给连续刻度“;
我已经摸索了一段时间,但让我从一开始 我的数据的前几行是这样的:Loops ggplot2+;循环结果为;错误:离散值提供给连续刻度“;,loops,ggplot2,Loops,Ggplot2,我已经摸索了一段时间,但让我从一开始 我的数据的前几行是这样的: 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 198
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
它们都是数字
现在,我只想用ggplot()+geom_line()
做一个绘图,以可视化每种类型一年的变化。它在一步一步进行时起作用,即:
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仅包含除年份
之外的所有列名)
但是,在for循环中执行此操作:
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]))
}
p1将带有for循环的行添加到ggplot
对象可能会导致报告的错误消息,但由于延迟求值而导致一般问题。这是经常被问到的问题,请参见,例如,或
但是,以长格式提供数据时,ggplot2
效果最佳。这里,来自数据的melt()
。表包用于重塑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()
库(data.table)
将带有for循环的行添加到ggplot
对象可能会导致报告的错误消息,但由于延迟求值而导致一般问题。这是经常被问到的问题,请参见,例如,或
但是,以长格式提供数据时,ggplot2
效果最佳。这里,来自数据的melt()
。表包用于重塑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()
库(data.table)
熔化的