-3

I have a dataframe with days as the column header and id as the row header with the count of observations in each cell. I need to restructure the data so it can be used to create a scatterplot.

example data:

    day1  day2  day3  day4
ID1 1      3     4     1
r2evans
  • 141,215
  • 6
  • 77
  • 149
swarner
  • 21
  • 5
  • 1
    Welcome to Stack Overflow. Please review [how to ask](https://stackoverflow.com/help/how-to-ask) questions here on SO and what you can do to provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In short, please show us reproducible sample data, a code attempt, and where your code failed. – Maurits Evers Apr 12 '19 at 04:00

1 Answers1

1

I've added a few lines to your example data


library(ggplot2)
library(reshape2)

df <- read.table(text = 
"day1  day2  day3  day4
ID1 1      3     4     1
ID2 2      4     1     3
ID3 1      1     3     4", header = TRUE)

df %>%
  as.matrix %>%
  reshape2::melt(value.name = "count") -> new_df

   Var1 Var2 count
1   ID1 day1     1
2   ID2 day1     2
3   ID3 day1     1
4   ID1 day2     3
5   ID2 day2     4
6   ID3 day2     1
7   ID1 day3     4
8   ID2 day3     1
9   ID3 day3     3
10  ID1 day4     1
11  ID2 day4     3
12  ID3 day4     4

Now you can use it for a plot, grouped by ID .


  ggplot(data = new_df) +
  geom_jitter(aes(x = Var2, y = count, color = Var1), width = 0.2, height = 0.2) + 
  xlab("day")

enter image description here

Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34