[英]Strings in ggplot x-axis
I'm trying to create a graph in R like this: 我正在尝试在R中创建一个图形,如下所示:
I have three columns (online, offline and routes). 我有三列(在线,离线和路线)。 However, when I add the following code: 但是,当我添加以下代码时:
library(ggplot2)
ggplot(coefroute, aes(routes,offline)) + geom_line()
I get the following message: 我收到以下消息:
geom_path: Each group consists of only one observation. geom_path:每个组仅包含一个观测值。 Do you need to adjust the group aesthetic? 您是否需要调整小组审美?
sample of coefroute: coefroute示例:
routes online offline
(Intercept) 210.4372 257.215
route10 7.543 30.0182
route100 18.3794 1.5313
route11 38.6537 78.8655
route12 66.501 94.8838
route13 -22.2391 -25.8448
route14 24.3652 177.7728
route15 48.5464 51.126 ...
routes: char, online and offline: num 路线:char,在线和离线:num
Can anybody help me with putting strings in x-axis in R? 有人可以帮我在R的x轴上放置字符串吗? Thank you! 谢谢!
In the absence of sample data, here's some toy data that has the same structure as yours: 在没有样本数据的情况下,以下一些玩具数据与您的玩具具有相同的结构:
coefroute <- data.frame(routes = c("A","B","C","D","E"),
online = c(21,26,30,15,20),
offline = c(15,20,7,12,15))
To replicate your example graph in ggplot2
you would want your data in a long format, so that you can group on offline/online. 要在ggplot2
复制示例图,您需要使用长格式的数据,以便可以离线/在线分组。 See more here: Plotting multiple lines from a data frame with ggplot2 and http://ggplot2.tidyverse.org/reference/aes.html . 在此处查看更多信息: 使用ggplot2和http://ggplot2.tidyverse.org/reference/aes.html 从数据框中绘制多行 。
You can rearrange your data into a long format very easily with lots of different functions or packages, but a standard approach is to use gather
from tidyr
and group your series for online
and offline
into something called, say, status
or whatever you want. 您可以使用许多不同的功能或程序包很容易地将数据重新排列为长格式,但是一种标准方法是使用tidyr
gather
,并将您的系列online
和offline
分组为status
或您想要的任何东西。
library(tidyr)
coefroute <- gather(coefroute, key = status, value = coef, online:offline)
Then you can plot this easily in ggplot: 然后,您可以在ggplot中轻松绘制此图:
library(ggplot2)
ggplot(coefroute, aes(x = routes, y = coef, group = status, colour = status))
+ geom_line() + scale_x_discrete()
That should create something like your example graph. 那应该创建类似示例图的东西。 You may want to modify the colours, captions, etc. There's lots of documentation about these things that's easy enough to find. 您可能想要修改颜色,标题等。关于这些内容的大量文档很容易找到。 I've added scale_x_discrete()
here so that ggplot knows to treat your x variable as a discrete one. 我在这里添加了scale_x_discrete()
,以便ggplot知道将x变量视为离散变量。
Secondly, my suspicion is that a line plot may be less effective than geoms in communicating what you're trying to communicate here. 其次,我的怀疑是,在传达您要在此处传达的内容时,折线图可能不如几何图形有效。 I would perhaps use geom_bar(stat = "identity", position = "dodge")
in place of geom_line
. 我可能会使用geom_bar(stat = "identity", position = "dodge")
代替geom_line
。 That would create a vertical bar chart for each coefficient with offline and online coefficients side by side. 这将为每个系数与离线和在线系数并排创建垂直条形图。
ggplot(coefroute, aes(x = routes, y = coef, group = status, fill = status))
+ geom_bar(stat = "identity", position = "dodge") + scale_x_discrete()
There are two approaches: 有两种方法:
# using dshkol's toy data
coefroute <- data.frame(routes = c("A","B","C","D","E"),
online = c(21,26,30,15,20),
offline = c(15,20,7,12,15))
library(ggplot2)
# plotting data in wide format (not recommended)
ggplot(coefroute, aes(x = routes, group = 1L)) +
geom_line(aes(y = online), colour = "blue") +
geom_line(aes(y = offline), colour = "orange")
This approach has several drawbacks. 这种方法有几个缺点。 Each variable needs its own call to geom_line()
and there is no legend. 每个变量都需要对geom_line()
的调用,并且没有图例。
For reshaping, the melt()
is used which is available from the reshape2
package (the predecessor of the tidyr
/ dplyr
packages) or in a faster implementation form the data.table
package. 为了进行重塑,使用了melt()
,可从reshape2
包( tidyr
/ dplyr
包的前身)或以更快的实现方式从data.table
包中获得。
ggplot(data.table::melt(coefroute, id.var = "routes"),
aes(x = routes, y = value, group = variable, colour = variable)) +
geom_line()
Note that in both cases the group
aesthetic has to be specified because the x-axis is discrete. 请注意,在两种情况下,都必须指定group
美观度,因为x轴是离散的。 This tells ggplot to consider the data points belonging to one series despite the discrete x values. 这告诉ggplot考虑x值离散的情况下属于一个系列的数据点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.