1

I actually need help building on this question: ggplot2 graphic order by grouped variable instead of in alphabetical order. I need to produce a similar graph and I actually have a problem with the black points. I have data where column names are dates and rows are filled with 0 or 1 and I need to plot the point if the value is 1. To reproduce, here is a small sample (in my dataset, there is over 300 columns):

df <- data.frame(id=c(1,2,3),
                 "26April1970"=c(0,0,1),
                 "14August1970"=c(0,1,0))

I need to plot the dates on the x axis, match the id to the canton and show the points where the value is 1. Could anyone help?

RRR
  • 11
  • 2
  • I don't think that question you link is at all related to your question. To use `ggplot` effectively you need to put your data in a long format. (That is, you want dates on the x-axis? Then you need one column with dates in it - not dates as the names of many columns.). See [this FAQ](https://stackoverflow.com/q/2185252/903061) about reshaping data from wide to long. Get your data in long format, then convert your dates to actual `Date` class objects, and building the plot will be easy. – Gregor Thomas Oct 22 '20 at 16:37

2 Answers2

1

Try this:

plot_data = df %>% 
  ## put data in long format
  pivot_longer(-id, names_to = "colname") %>%
  ## keep only 1s
  filter(value == 1) %>%
  ## convert dates to Date class
  mutate(date = as.Date(colname, format = "%d%B%Y"))
plot_data
# # A tibble: 2 x 4
#      id colname      value date      
#   <dbl> <chr>        <dbl> <date>    
# 1     2 14August1970     1 1970-08-14
# 2     3 26April1970      1 1970-04-26


## plot
ggplot(plot_data, aes(x = date, y = factor(id))) +
  geom_point()

enter image description here


Using this data:

df <- data.frame(id=c(1,2,3),
                 "26April1970"=c(0,0,1),
                 "14August1970"=c(0,1,0), check.names = FALSE)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
0

Maybe you are looking for this:

library(ggplot2)
library(dplyr)
library(tidyr)
#Data
df <- data.frame(id=c(1,2,3),
                 "26April1970"=c(0,0,1),
                 "14August1970"=c(0,1,0))
#Code
df %>% pivot_longer(-id) %>%
  ggplot(aes(x=name,y=factor(value)))+
  geom_point(aes(color=factor(value)))+
  scale_color_manual(values=c('transparent','black'))+
  theme(legend.position = 'none')+xlab('Date')+ylab('value')

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84
  • @RRR Understood you have 20 variables so made this change `scale_color_manual(values=c('transparent',rep('black',19)))` – Duck Oct 22 '20 at 16:59
  • @RRR Let me know how that goes! Also it looks like you have a different data to that posted! – Duck Oct 22 '20 at 17:00
  • @RRR It is your data, could you please use `dput(yourdata)` and paste the output in the question with the editing option? – Duck Oct 22 '20 at 17:41
  • Why are you map anything to `color`? OP wants all points black, so delete `scale_color_manual`, delete `color = factor(value)`, and put `color = "black"` in the `geom_point` layer outside of `aes()`. – Gregor Thomas Oct 22 '20 at 17:47