I'm trying to create line plots of four series of percentages using ggplot. Sample data included below ... the output should have "Week" as the x axis and the numeric percentages for each series as a separate y-axis line plot.
I've tried to follow the instructions at Plot multiple columns on the same graph in R. The dataframe melts just fine but plot fails.
Data for this test: save the following as "pct.txt" and use dget per the code provided.
structure(list(NoRep = c("2.2%", "10.7%", "7.9%", "100%", "100%", "5.6%", "2.9%", "3%", "3.9%", "4.7%", "3.9%", "3.1%", "2%", "2.4%", "2.4%", "17.7%", "2.1%", "7.4%", "1.9%", "8%"), Report = c("46.5%", "33.4%", "36.4%", "0%", "0%", "42.2%", "43.8%", "39.2%", "36.8%", "36.1%", "36.1%", "35.8%", "37.7%", "26.6%", "26.9%", "53.4%", "39.9%", "32.9%", "40.9%", "41.9%"), Mobile_Internet = c("14.4%", "13.6%", "1.2%", "0%", "0%", "2.4%", "11.7%", "11%", "10.2%", "10.1%", "9.1%", "39.8%", "39%", "39.2%", "38.8%", "16.8%", "31.1%", "29.2%", "28.8%", "27.4%"), Mobile_SMS = c("36.9%", "42.3%", "54.5%", "0%", "0%", "49.9%", "41.6%", "46.8%", "49.1%", "49%", "50.9%", "21.3%", "21.2%", "31.9%", "31.9%", "12%", "26.9%", "30.5%", "28.3%", "22.7%"), Week = c("2018_W48", "2018_W49", "2018_W50", "2018_W51", "2018_W52", "2019_W01", "2019_W02", "2019_W03", "2019_W04", "2019_W05", "2019_W06", "2019_W07", "2019_W08", "2019_W09", "2019_W10", "2019_W11", "2019_W12", "2019_W13", "2019_W14", "2019_W15" )), .Names = c("NoRep", "Report", "Mobile_Internet", "Mobile_SMS", "Week"), row.names = c("2018_W48", "2018_W49", "2018_W50", "2018_W51", "2018_W52", "2019_W01", "2019_W02", "2019_W03", "2019_W04", "2019_W05", "2019_W06", "2019_W07", "2019_W08", "2019_W09", "2019_W10", "2019_W11", "2019_W12", "2019_W13", "2019_W14", "2019_W15"), class = "data.frame")
test <- dget(file="pct.txt")
tmp <- melt(test, id.vars = "Week")
ggplot(tmp, aes(Week, value, col=variable))+
geom_line()
I'm getting the error "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?"
答案 0 :(得分:1)
The error message was correct, all you have to do is add the group aesthetic.
library(ggplot2)
pct <- structure(list(NoRep = c("2.2%", "10.7%", "7.9%", "100%", "100%", "5.6%", "2.9%", "3%", "3.9%", "4.7%", "3.9%", "3.1%", "2%", "2.4%", "2.4%", "17.7%", "2.1%", "7.4%", "1.9%", "8%"), Report = c("46.5%", "33.4%", "36.4%", "0%", "0%", "42.2%", "43.8%", "39.2%", "36.8%", "36.1%", "36.1%", "35.8%", "37.7%", "26.6%", "26.9%", "53.4%", "39.9%", "32.9%", "40.9%", "41.9%"), Mobile_Internet = c("14.4%", "13.6%", "1.2%", "0%", "0%", "2.4%", "11.7%", "11%", "10.2%", "10.1%", "9.1%", "39.8%", "39%", "39.2%", "38.8%", "16.8%", "31.1%", "29.2%", "28.8%", "27.4%"), Mobile_SMS = c("36.9%", "42.3%", "54.5%", "0%", "0%", "49.9%", "41.6%", "46.8%", "49.1%", "49%", "50.9%", "21.3%", "21.2%", "31.9%", "31.9%", "12%", "26.9%", "30.5%", "28.3%", "22.7%"), Week = c("2018_W48", "2018_W49", "2018_W50", "2018_W51", "2018_W52", "2019_W01", "2019_W02", "2019_W03", "2019_W04", "2019_W05", "2019_W06", "2019_W07", "2019_W08", "2019_W09", "2019_W10", "2019_W11", "2019_W12", "2019_W13", "2019_W14", "2019_W15" )), .Names = c("NoRep", "Report", "Mobile_Internet", "Mobile_SMS", "Week"), row.names = c("2018_W48", "2018_W49", "2018_W50", "2018_W51", "2018_W52", "2019_W01", "2019_W02", "2019_W03", "2019_W04", "2019_W05", "2019_W06", "2019_W07", "2019_W08", "2019_W09", "2019_W10", "2019_W11", "2019_W12", "2019_W13", "2019_W14", "2019_W15"), class = "data.frame")
pct_long <- tidyr::gather(pct, "variable", "value", -c("Week"))
ggplot(pct_long, aes(Week, value, col = variable, group = variable)) + geom_line()
Note that including the % signs in your data means that ggplot didn't know how to properly plot the y-axis. Replacing that after loading the data is relatively trivial using gsub
.
pct_long$value_numeric <- as.numeric(gsub("%", "", pct_long$value))
ggplot(pct_long, aes(Week, value_numeric, col = variable, group = variable)) + geom_line()