作者:别被风景迷了眼 | 来源:互联网 | 2023-09-14 11:25
Imtryingtodrawaplotwithseveralcurvesinit.Thex-axisarenotnumericalvalues,butString
I'm trying to draw a plot with several curves in it. The x-axis are not numerical values, but Strings.
我正在尝试绘制一条带有几条曲线的情节。 x轴不是数值,而是字符串。
This works fine (like in how to plot all the columns of a data frame in R):
这很好用(比如如何在R中绘制数据帧的所有列):
require(ggplot2)
df_ok <- rbind(data.frame(x=4:1,y=rnorm(4),d="d1"),data.frame(x=3:1,y=rnorm(3),d="d2"))
ggplot(df_ok, aes(x,y)) + geom_line(aes(colour=d))
But my data looks like this:
但我的数据看起来像这样:
require(ggplot2)
df_nok <- rbind(data.frame(x=c("four","three","two","one"),y=rnorm(4),d="d1"),data.frame(x=c("three","two","one"),y=rnorm(3),d="d2"))
ggplot(df_nok, aes(x,y)) + geom_line(aes(colour=d))
I get the error geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?. Even though the graph lines do not appear, the axis are plotted, and the x-Axis contains the correct Labels - but also in wrong order.
我得到错误geom_path:每组只包含一个观察。你需要调整团体美学吗?即使没有出现图形线,也会绘制轴,并且x轴包含正确的标签 - 但也是错误的顺序。
Any idea how to plot this as easy as possible? (Also note the missing x-values for some series).
知道如何尽可能简单地绘制这个图吗? (另请注意某些系列的缺失x值)。
3 个解决方案