這裏有一個快速可視化GGPLOT2和plotly通過@KppatelPatel 所建議的ggplotly輸出將是你的圖形用戶界面上的互動情節,具有懸停信息例如Month: Apr-16; Percentage: 2.34; Gender: F
library(ggplot2)
library(plotly)
p <- ggplot(dat, aes(x=Month, y=Percentage, fill=Gender)) +
geom_bar(stat="identity", position = position_dodge()) +
facet_wrap(~Age, ncol=2)
ggplotly(p)
的data.frame dput對所提供的數據:
dat <- structure(list(Month = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 3L, 3L, 3L, 3L), .Label = c("Apr-16", "Mar-16", "May-16"), class = "factor"),
Age = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L), .Label = c("0-20", "21-30"), class = "factor"),
Gender = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L), .Label = c("F", "M"), class = "factor"), Percentage = c(1.01,
0.46, 5.08, 4.03, 2.34, 3.55, 6.78, 9.08, 3.56, 3, 2.08,
10)), .Names = c("Month", "Age", "Gender", "Percentage"), class = "data.frame", row.names = c(NA,
-12L))
編輯:
要繪製時間的邏輯順序,轉換本月至今格式:
library(dplyr)
dat$Time <- dat$Month %>%
as.character %>%
paste("01-", .) %>%
as.Date(., format= "%d-%b-%y")
與x=Time
繪製的相同ggplot上述會給你以下幾點:
可以ggplotly&GGPLOT2使用,並且組由年齡和性別的顏色,那麼你可以繪X =月和y =百分比。 gglotlot會給你想要的交互。 ggplot2將創建好的方式來創建情節 –