R-使用ggplot作折线图-无折线显示

时间:2018-07-25 03:17:21

标签: r ggplot2

我正在使用ggplot和geom_line,但是没有显示任何行。

我使用了以下命令:

library(ggplot2)

ggplot(campaigns, aes(x=Send.Time, y=Open.Rate, color='red')) + geom_line()

请参见下面的屏幕截图

see screenshot

1 个答案:

答案 0 :(得分:0)

您应该从字符串中删除“%”并转换为数字。请参见下面的代码:

# Simulation
library(lubridate)
library(dplyr)

campaigns <- data.frame(
Send.Time = c("5/30/17", "6/1/17", "6/1/17", "6/6/17", "6/8/17", "6/15/17"),
Open.Rate = c("33.40%", "9.14%", "29.64%", "24.90%", "8.07%", "32.44%")
)


# convert to numeric
campaigns <- campaigns %>% mutate(Open.Rate = as.numeric(gsub("%", "", as.character(Open.Rate))))

# plot 
campaigns %>% ggplot(aes(x = as.numeric(Send.Time), y = Open.Rate), colour = "red") +
  geom_line() +
  scale_x_continuous(labels = campaigns$Send.Time, breaks = seq_along(campaigns$Send.Time)) +
  geom_point() +
  xlab("Session date")

输出: Plot