[英]Grouped bar chart with date on x-axis
I'm getting back to R, and I have some trouble plotting the data I want. 我回到R,并且在绘制所需数据时遇到一些麻烦。
It's in this format : 格式如下:
date value1 value2
10/25/2016 50 60
12/16/2016 70 80
01/05/2017 35 45
And I would like to plot value1 and value2 next to each other, with the corresponding date on the x axis. 而且我想在x轴上以彼此对应的日期绘制value1和value2。 So far I have this, I tried to plot only value1 first : 到目前为止,我已经尝试过只绘制value1:
df$date <- as.Date(df$date, "%m/%d/%Y")
ggplot(data=df,aes(x=date,y=value1))
But the resulting plot doesn't show anything. 但是结果情节什么都没显示。 The maximum values on the x and y axis seem to correspond to the ranges of my dataframe, but why is nothing showing up? x和y轴上的最大值似乎与我的数据框的范围相对应,但是为什么什么都没有显示?
It works with plot(df$date,df$value1)
though, so I don't get what I am doing wrong. 它可以与plot(df$date,df$value1)
,所以我不会做错什么。
the ggplot call alone does not actually create any layers on the plot. 仅ggplot调用实际上不会在绘图上创建任何图层。 You need to add a geom
. 您需要添加一个geom
。
For this you probably want geom_point()
or geom_line()
为此,您可能需要geom_point()
或geom_line()
ggplot(data=df,aes(x=date,y=value1)) +
geom_point()
or 要么
ggplot(data=df,aes(x=date,y=value1)) +
geom_line()
or you could do both if you want points and lines 或者如果您想要点和线也可以同时做
ggplot(data=df,aes(x=date,y=value1)) +
geom_point() +
geom_line()
If you want both values on the plot, I would recommend doing some data manipulation first with the tidyr
package. 如果您想在绘图上同时使用这两个值,建议您先使用tidyr
软件包进行一些数据处理。
df %>%
gather(key = "group", value = "value", value1:value2) %>%
ggplot(aes(date, value, color = group, group = group)) +
geom_line()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.