![](/img/trans.png)
[英]How can I control y-axis ticks and x-axis ticks independently in ggplot2?
[英]How can I plot date on x-axis and price list on y-axis and attribute by using ggplot?
我正在尝试使用ggplot
绘制包含在以下日期框架中的数据:
df <- data.frame( dress_id = c(1,2,3,4,5),
29/8/2013 = c(2000,150,6,1000,900),
31/8/2013 = c(2000,200,7,1100,1000),
2/9/2013 = c(2400,600,7,1350,1300),
4/9/2013 = c(2600,600,7,1500,1400),
style = c("Sexy", "Casual","vintage","Brief","cute"))
我希望 x 轴是我的日期 (29/8/2013...2/9/2013),我的 y 轴是日期的销售价格,最后是我的风格。
这可以使用ggplot吗?
这是zx8754答案的详细信息。 首先,请注意我在日期列前面放了一个X
:这是因为 R 中的列名不应以数字开头。
df <- data.frame( dress_id = c(1,2,3,4,5),
"X29/8/2013" = c(2000,150,6,1000,900),
"X31/8/2013" = c(2000,200,7,1100,1000),
"X2/9/2013" = c(2400,600,7,1350,1300),
"X4/9/2013" = c(2600,600,7,1500,1400),
style = c("Sexy", "Casual","vintage","Brief","cute"))
接下来,我加载tidyverse
包,其中包含使用 data.frames 的函数,还包括ggplot2
library(tidyverse)
最后,我将您的数据从宽转换为长:这是通过gather
函数完成的。 因此,您的 data.frame 中现在有一个包含所有当前日期的日期列和一个包含销售价格的值列。
df %>%
gather(date, value, -dress_id, -style) %>%
mutate(date = as.Date(date, format = c("X%d.%m.%Y"))) %>%
ggplot(aes(x = date, y = value, colour = style)) +
geom_line()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.