![](/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.