ggplot2 (R):错误msg:“错误:美学必须是长度1或与数据相同(1093):x, y'

时间:2022-06-21 16:11:13

I started learning to use R two days ago, so this is probably a really easy fix!

我两天前开始学习使用R,所以这可能是一个很简单的解决方法!

I started by making this very simple table, showing the %ages of 'g' and 't' in my data (I'm working in socio-linguistics).

我开始做这个非常简单的表格,在我的数据中显示“g”和“t”的百分比(我在社会语言学中工作)。

    ---g---  | ---t--- 
    0.936871 | 0.063129 

I'm trying to turn this into a bar graph using ggplot2. I want the x-axis to have two categories, with one bar representing 'g' and one representing 't', side by side rather than stacked. I want the y-axis to show the two %ages.

我想用ggplot2把这个变成条形图。我希望x轴有两个类别,一个代表g,一个表示t,并排而不是堆叠。我想让y轴显示2%的年龄。

I've tried various different scripts but I keep getting the error message 'Aesthetics must be either length 1 or the same as in the data'. What does this error message mean and how can I fix it?

我尝试过各种不同的脚本,但我一直得到的错误信息“美学必须是长度1或与数据相同”。这个错误消息意味着什么,我该如何修复它?

Thanks! Sadie

谢谢!赛迪

1 个解决方案

#1


0  

You're getting that error because you don't have your dataset in the right form. With your data you've a t variable and g variable. ggplot wants to receive aes values that depict all the elements of that axis/feature.

你会得到那个错误,因为你没有正确的数据集。你的数据有一个t变量和g变量。ggplot希望接收描述该axis/特性的所有元素的aes值。

The data that you have is in wide format (many columns). ggplot2 utilizes long format data (key-value pairs) better. To transform your wide data to long data you use the tidyr package (specifically the gather function) to gather values into a column of variables and a column of values. In your case the variables would be g and t and the values would be your percentages.

您拥有的数据具有广泛的格式(许多列)。ggplot2更好地利用长格式数据(键值对)。要将宽数据转换为长数据,可以使用tidyr包(特别是集合函数)将值收集到一个变量列和一个值列。在你的例子中,变量是g和t,值是你的百分比。

You'd then pass that transformed data into ggplot, specifying the x-axis as the variable, the y-axis as the percentage. You want to plot it as a bar with geom_bar() setting stat = "identity" because you're supplying the values. The last step is setting the labels on the y-axis to percent via scale_y_continuous.

然后将转换后的数据转换为ggplot,指定x轴为变量,y轴为百分比。您希望将其作为一个bar与地_bar()设置stat = "identity",因为您提供了这些值。最后一步是通过scale_y_continuous将y轴上的标签设置为百分数。

library(tidyr)
library(ggplot2)
library(scales)

df <- data.frame(g = 0.936871,
                 t = 0.063129)

df <- gather(df, variable, value)

ggplot(df, aes(x = variable, y = value)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = percent)

The result looks like this:

结果是这样的:

ggplot2 (R):错误msg:“错误:美学必须是长度1或与数据相同(1093):x, y'

As to your comment request:

关于你的评论请求:

df <- data.frame(
  category = c(1, 2),
  g = c(0.8070175, 0.97099768),
  t = c(0.1929825, 0.029000232)
)

df <- gather(df, variable, value, -category)

ggplot(df, aes(x = category, y = value)) +
  geom_col(aes(fill = variable), position = position_dodge()) +
  scale_y_continuous(labels = percent) +
  scale_x_discrete(limits = c(1,2), breaks = c(1,2), labels = c("Category 1", "Category 2"))

ggplot2 (R):错误msg:“错误:美学必须是长度1或与数据相同(1093):x, y'

#1


0  

You're getting that error because you don't have your dataset in the right form. With your data you've a t variable and g variable. ggplot wants to receive aes values that depict all the elements of that axis/feature.

你会得到那个错误,因为你没有正确的数据集。你的数据有一个t变量和g变量。ggplot希望接收描述该axis/特性的所有元素的aes值。

The data that you have is in wide format (many columns). ggplot2 utilizes long format data (key-value pairs) better. To transform your wide data to long data you use the tidyr package (specifically the gather function) to gather values into a column of variables and a column of values. In your case the variables would be g and t and the values would be your percentages.

您拥有的数据具有广泛的格式(许多列)。ggplot2更好地利用长格式数据(键值对)。要将宽数据转换为长数据,可以使用tidyr包(特别是集合函数)将值收集到一个变量列和一个值列。在你的例子中,变量是g和t,值是你的百分比。

You'd then pass that transformed data into ggplot, specifying the x-axis as the variable, the y-axis as the percentage. You want to plot it as a bar with geom_bar() setting stat = "identity" because you're supplying the values. The last step is setting the labels on the y-axis to percent via scale_y_continuous.

然后将转换后的数据转换为ggplot,指定x轴为变量,y轴为百分比。您希望将其作为一个bar与地_bar()设置stat = "identity",因为您提供了这些值。最后一步是通过scale_y_continuous将y轴上的标签设置为百分数。

library(tidyr)
library(ggplot2)
library(scales)

df <- data.frame(g = 0.936871,
                 t = 0.063129)

df <- gather(df, variable, value)

ggplot(df, aes(x = variable, y = value)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = percent)

The result looks like this:

结果是这样的:

ggplot2 (R):错误msg:“错误:美学必须是长度1或与数据相同(1093):x, y'

As to your comment request:

关于你的评论请求:

df <- data.frame(
  category = c(1, 2),
  g = c(0.8070175, 0.97099768),
  t = c(0.1929825, 0.029000232)
)

df <- gather(df, variable, value, -category)

ggplot(df, aes(x = category, y = value)) +
  geom_col(aes(fill = variable), position = position_dodge()) +
  scale_y_continuous(labels = percent) +
  scale_x_discrete(limits = c(1,2), breaks = c(1,2), labels = c("Category 1", "Category 2"))

ggplot2 (R):错误msg:“错误:美学必须是长度1或与数据相同(1093):x, y'