简体   繁体   English

错误:美学必须是长度1或与数据相同(n):填充

[英]Error: Aesthetics must be either length 1 or the same as the data (n): fill

I want to add a line (from the data of a second dataframe) to the plot of a first dataframe.我想在第一个 dataframe 的 plot 中添加一行(来自第二个数据帧的数据)。 Here they are:他们来了:

Year<-c(rep(2001,5),rep(2002,5),rep(2003,5),rep(2004,5),rep(2005,5))
name<-c("John","Ellen","Mark","Randy","Luisa")
Name<-c(rep(name,5))
Value<-sample(seq(0,25,by=1),25)
mydata<-data.frame(Year,Name,Value)

Year_2<-c(2001:2005)
Value_2<-c(1,0,10,3,7)
mydata_2<-data.frame(Year_2,Value_2)

This is the plot I want to obtain from the first dataframe:这是我想从第一个 dataframe 获得的 plot:

plot<-ggplot(mydata, aes(fill=Name, y=Value, x=Year)) +
  geom_bar(position="stack", stat="identity") + 
  scale_color_viridis(discrete = TRUE)+
  scale_fill_viridis(discrete = TRUE) +
  theme(axis.line = element_blank(),
        axis.text.x=element_text(size=11,margin=margin(b=10)),
        axis.text.y=element_text(size=11,margin=margin(l=10)),
        axis.ticks.y = element_blank(),
        axis.title=element_text(size=18,face="bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.text = element_text(size=14,face = "italic"),
        legend.title = element_text(size=18))

And I tried to add the line from the second dataframe, in this way:我尝试从第二个 dataframe 中添加该行,以这种方式:

plot +  geom_line(data=mydata_2, size=1, aes(x=Year_2,y=Value_2))

Getting this error, which I do not really understand:得到这个错误,我不太明白:

Error: Aesthetics must be either length 1 or the same as the data (5): fill

I guess it's because mydata is of length 25 and mydata_2 is 5. But what does fill has to do with this?我想这是因为mydata的长度为 25,而mydata_2的长度为 5。但是fill与此有什么关系? Actually, inserting a fill argument partially solves the problem:实际上,插入一个fill参数部分地解决了这个问题:

plot +  geom_line(data=mydata_2, size=1, aes(x=Year_2,y=Value_2,fill="red"))

Now the plot has the desired line, but I cannot seem to adjust the aesthetic of the line itself (eg the colour, that is actually black and not red, or other parameters I tried to add).现在 plot 具有所需的线条,但我似乎无法调整线条本身的美感(例如颜色,实际上是黑色而不是红色,或者我尝试添加的其他参数)。 Anyway, I tried adjusting the length of mydata_2 :无论如何,我尝试调整mydata_2的长度:

Year_3<-c(rep(2001,5),rep(2002,5),rep(2003,5),rep(2004,5),rep(2005,5))
Value_3<-c(1,0,0,0,0,3,0,0,0,0,5,0,0,0,0,0,0,0,0,0,11,0,0,0,0)
mydata_3<-data.frame(Year_3,Value_3) 

Now the line I want is regularly plotted, but with another line following the X-axis.现在我想要的线定期绘制,但在 X 轴之后有另一条线。 I did not try anything else.我没有尝试其他任何东西。

Is there a way to plot mydata_2 (thus without modifying the length, as in mydata_3 ) and/or to delete the line on the X-axis in mydata_3 ?有没有办法 plot mydata_2 (因此不修改长度,如mydata_3 )和/或删除mydata_3 X 轴上的线?

Try this:尝试这个:

ggplot() +
  geom_bar(data = mydata, aes(fill=Name, y=Value, x=Year), position="stack", stat="identity") + 
  scale_color_viridis(discrete = TRUE)+
  scale_fill_viridis(discrete = TRUE) +
  theme(axis.line = element_blank(),
        axis.text.x=element_text(size=11,margin=margin(b=10)),
        axis.text.y=element_text(size=11,margin=margin(l=10)),
        axis.ticks.y = element_blank(),
        axis.title=element_text(size=18,face="bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.text = element_text(size=14,face = "italic"),
        legend.title = element_text(size=18)) +   
  geom_line(data=mydata_2, aes(x=Year_2,y=Value_2), size=1, color="red")

You get that error because you called aes() inside the first ggplot() , so it tries to look for Name , Value and Year .您会收到该错误,因为您在第一个ggplot()中调用了aes() ),因此它会尝试查找NameValueYear

One way to get around is to specify inherit.aes=FALSE in second geom_line :一种解决方法是在第二个geom_line中指定inherit.aes=FALSE

ggplot(mydata, aes(fill=Name, y=Value, x=Year)) +
geom_bar(position="stack", stat="identity") + 
scale_color_viridis(discrete = TRUE)+
scale_fill_viridis(discrete = TRUE) +
geom_line(data=mydata_2,inherit.aes=FALSE,aes(x=Year_2,y=Value_2))

Or you call data= separately in each geom:或者您在每个几何图形中分别调用data=

ggplot() +
geom_bar(data = mydata, aes(fill=Name, y=Value, x=Year),
position="stack", stat="identity") + 
scale_color_viridis(discrete = TRUE)+
scale_fill_viridis(discrete = TRUE) +
geom_line(data=mydata_2,aes(x=Year_2,y=Value_2))

Both of which gives:两者都给出:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:美学的长度必须为1或与数据相同(4) - Error: Aesthetics must be either length 1 or the same as the data (4) 美学必须是长度 1 或与数据 (138) 相同:填充 - Aesthetics must be either length 1 or the same as the data (138): fill 绘制 Shapefile 美学必须是长度 1 或与带填充的数据相同 - Plotting Shapefile Aesthetics must be either length 1 or the same as data with fill ggplot故障排除:错误:美学长度必须为1或与数据(24)相同:x,y,填充 - ggplot troubleshoot: Error: Aesthetics must be either length 1 or the same as the data (24): x, y, fill R: Ggplot2 错误:美学必须是长度1或与数据相同(72):填充 - R: Ggplot2 Error: Aesthetics must be either length 1 or the same as the data (72): fill 条形图ggplot2-错误:美学必须为长度1或与数据(150)相同:fill,x,y - Bar plot ggplot2 - Error: Aesthetics must be either length 1 or the same as the data (150): fill, x, y 用选择的颜色和错误填充条形图:美学长度必须为1或与数据相同 - Fill bar chart with choosed Color and Error: Aesthetics must be either length 1 or the same as the data 错误:Aesthetics 必须是长度为 1 或与数据 (13) 相同:ymax - Error: Aesthetics must be either length 1 or the same as the data (13): ymax “错误:美学的长度必须为1或与数据相同”为什么? - “Error: Aesthetics must be either length 1 or the same as the data” Why? 错误:美学必须是长度 1 或与数据 (5) 相同:y - Error: Aesthetics must be either length 1 or the same as the data (5): y
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM