2d plot with 3rd variable as color in RStudio

*爱你&永不变心* 提交于 2019-12-13 22:58:54

问题


I have a dataset as CSV with three columns:

  • timestamp (e.g. 2018/12/15)
  • keyword (e.g. "hello")
  • count (e.g. 7)

I want one plot where all the lines of the same keyword are connected with each other and timestamp is on the X- and count is on the Y- axis. I would like each keyword to have a different color for its line and the line being labeled with the keyword.

The CSV has only ~30.000 rows and R runs on a dedicated machine. Performance can be ignored.

I tried various approaches with mathplot and ggplot in this forum, but didn't get it to work with my own data.

What is the easiest solution to do this in R?

Thanks!

EDIT:

I tried customizing Romans code and tried the following:

`csvdata <- read.csv("c:/mydataset.csv", header=TRUE, sep=",")  

time <- csvdata$timestamp  
count <- csvdata$count  
keyword <- csvdata$keyword  

time <- rep(time)  
xy <- data.frame(time, word = c(keyword), count, lambda = 5)  

library(ggplot2)  

ggplot(xy, aes(x = time, y = count, color = keyword)) +  
  theme_bw() +  
  scale_color_brewer(palette = "Set1") +  # choose appropriate palette  
  geom_line()`

This creates a correct canvas, but no points/lines in it...

DATA:

head(csvdata)

keyword count  timestamp
1 non-distinct-word     3 2018/08/09
2 non-distinct-word     2 2018/08/10
3 non-distinct-word     3 2018/08/11

str(csvdata)

'data.frame':   121 obs. of  3 variables:
 $ keyword  : Factor w/ 10 levels "non-distinct-word",..: 5 5 5 5 5 5 5 5 5 5 ...
 $ count    : int  3 2 3 1 6 6 2 3 2 1 ...
 $ timestamp: Factor w/ 103 levels "2018/08/09","2018/08/10",..: 1 2 3 4 5 6 7 8 9 10 ...

回答1:


Something like this?

# Generate some data. This is the part poster of the question normally provides.
today <- as.Date(Sys.time())
time <- rep(seq.Date(from = today, to = today + 30, by = "day"), each = 2)
xy <- data.frame(time, word = c("hello", "world"), count = rpois(length(time), lambda = 5))

library(ggplot2)

ggplot(xy, aes(x = time, y = count, color = word)) +
  theme_bw() +
  scale_color_brewer(palette = "Set1") +  # choose appropriate palette
  geom_line()



来源:https://stackoverflow.com/questions/53805037/2d-plot-with-3rd-variable-as-color-in-rstudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!