[英]Several graphs in ggplot2: Aesthetics must either be length one, or the same length
I use the following dataset (downloadable here ) and the code (down below) trying to plot several graphs in one ggplot. 我使用以下数据集(可从这里下载)和代码(下面的代码)尝试在一个ggplot中绘制几个图形。 I know that there are plenty of explanations out there, but still I do not seem to get the job done because I am confused about where to put the commands for ggplot to understand what I want.
我知道那里有很多解释,但我似乎还没有完成工作,因为我很困惑在哪里把ggplot的命令放到了解我想要的东西。
I addition, I know that there a two ways raw data could be present: either in wide or long format. 此外,我知道有两种方式可以存在原始数据:无论是宽格式还是长格式。 When I keep the data in wide format I have to write a lot in order to get the job done (see code and graph below), but when I convert it to the long format , ggplot complains about missing values (see code and error message down below).
当我以宽格式保存数据时,我必须编写很多内容才能完成工作(请参阅下面的代码和图表),但是当我将其转换为长格式时 ,ggplot会抱怨缺少值(请参阅代码和错误消息)在下面)。
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 I got so far, but I do not want to need to write so much (since I want to have 6 more graphs in this ggplot): 这是我到目前为止(唯一)成功的情节,但我不想写这么多(因为我想在这个ggplot中再增加6个图形):
I know that to use he long format would mean a more elegant way how to plot the multiplot but I what ever command I use (see above) I always get the following complain: 我知道使用他的长格式将意味着更优雅的方式如何绘制多重绘图但我使用的命令(见上文)我总是得到以下抱怨:
Error: Aesthetics must either be length one, or the same length as the dataProblems:time
错误:美学必须是长度为1或与dataProblems:time相同的长度
Does somebody know the answer to my question? 有人知道我的问题的答案吗?
To start with: your data have strings ".." in your supposedly numerical columns, which will convert the entire columns to class
character
(or factor
, depending on your stringsAsFactors
settings). 首先:您的数据在所谓的数字列中包含字符串“..”,它将整个列转换为
class
character
(或factor
,具体取决于您的stringsAsFactors
设置)。
If you wish to treat ".." as NA
, add na.strings = ".."
to your read.xxx
call. 如果您希望将“..”视为
NA
,请在read.xxx
调用中添加na.strings = ".."
。 This will ensure that the columns are treated as numeric
. 这将确保将列视为
numeric
。 str
should be your friend after you have read any data set. 读完任何数据后,
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.