Home > Mobile >  How to create plot with multiple entries
How to create plot with multiple entries

Time:09-28

I have the following dataset

p   start   end     Per     pm  on  IOI rat id
1   106.0   107.3   133.3   3.1 6   0.4 3.1 2
1   106.1   107.3   133.3   3.1 6   0.1 1.0 3
1   106.3   107.3   133.3   3.1 6   0.2 1.7 4
1   106.6   107.3   133.3   3.1 6   0.3 2.2 5
1   107.1   107.3   133.3   3.1 6   0.5 3.8 6
2   107.8   109.4   162.0   8.7 6   0.2 1.0 2
2   108.0   109.4   162.0   8.7 6   0.1 0.9 3
2   108.1   109.4   162.0   8.7 6   0.2 0.9 4
2   108.3   109.4   162.0   8.7 6   0.2 1.1 5
2   108.5   109.4   162.0   8.7 6   0.2 1.0 6
3   111.2   112.5   180.0   10.6    8   0.2 1.0 2
3   111.4   112.5   180.0   10.6    8   0.2 0.9 3
3   111.5   112.5   180.0   10.6    8   0.2 1.0 4
3   111.7   112.5   180.0   10.6    8   0.2 1.0 5
3   111.9   112.5   180.0   10.6    8   0.2 1.0 6
3   112.3   112.5   180.0   10.6    8   0.4 2.0 7
3   112.4   112.5   180.0   10.6    8   0.2 1.0 8
4   112.7   113.6   172.8   7.6     6   0.1 0.8 2
4   112.9   113.6   172.8   7.6     6   0.2 0.9 3
4   113.0   113.6   172.8   7.6     6   0.1 0.8 4
4   113.2   113.6   172.8   7.6     6   0.2 1.0 5
4   113.4   113.6   172.8   7.6      6  0.2 1.1 6
5   115.5   116.8   187.5   9.2     8   0.2 1.0 2
5   115.6   116.8   187.5   9.2     8   0.2 0.9 3
5   115.8   116.8   187.5   9.2     8   0.2 0.9 4
5   116.0   116.8   187.5   9.2     8   0.2 1.0 5
5   116.2   116.8   187.5   9.2     8   0.2 1.0 6
5   116.6   116.8   187.5   9.2     8   0.4 2.0 7
5   116.8   116.8   187.5   9.2     8   0.2 1.0 8
6   117.1   118.2   160.0   8.9     6   0.2 1.0 2
6   117.2   118.2   160.0   8.9     6   0.1 0.9 3
6   117.4   118.2   160.0   8.9     6   0.1 0.9 4
6   117.5   118.2   160.0   8.9     6   0.2 1.0 5
6   117.7   118.2   160.0   8.9     6   0.2 1.1 6
7   121.8   123.6   125.0   5.4     8   0.2 1.2 2
7   122.1   123.6   125.0   5.4     8   0.4 2.9 3
7   122.3   123.6   125.0   5.4     8   0.1 1.0 4
7   122.5   123.6   125.0   5.4     8   0.3 2.2 5

And I would like to create one scattered plot with multiple entries based on subsets from this df. x = id and y = rat. The subsets are determined by the column p, so I would create one plot with entries (id, rat) from phrases 2,4,7 aligned and displayed in different colours.

I've started by making subsets with the filter function

phrase2 <- dt %>%
  filter(dt$p == 2)

phrase4 <- dt %>%
  filter(dt$p == 4)

phrase7 <- dt %>%
  filter(dt$p == 7)

But I couldn't figure out how to create the plots and be able to control the colours of each subset (this becomes difficult if I combine them into one new dt, for instance)

CodePudding user response:

library(dplyr)
library(ggplot2)
dt_new <- dt %>% filter(p %in% c(2, 4, 7))

ggplot(dt_new, aes(id, rat, color = factor(p)))   
  geom_point()  
  scale_color_manual(values = c("firebrick3", "chartreuse", "orange"))

enter image description here

Using this data:

dt = read.table(text = 'p   start   end     Per     pm  on  IOI rat id
1   106.0   107.3   133.3   3.1 6   0.4 3.1 2
1   106.1   107.3   133.3   3.1 6   0.1 1.0 3
1   106.3   107.3   133.3   3.1 6   0.2 1.7 4
1   106.6   107.3   133.3   3.1 6   0.3 2.2 5
1   107.1   107.3   133.3   3.1 6   0.5 3.8 6
2   107.8   109.4   162.0   8.7 6   0.2 1.0 2
2   108.0   109.4   162.0   8.7 6   0.1 0.9 3
2   108.1   109.4   162.0   8.7 6   0.2 0.9 4
2   108.3   109.4   162.0   8.7 6   0.2 1.1 5
2   108.5   109.4   162.0   8.7 6   0.2 1.0 6
3   111.2   112.5   180.0   10.6    8   0.2 1.0 2
3   111.4   112.5   180.0   10.6    8   0.2 0.9 3
3   111.5   112.5   180.0   10.6    8   0.2 1.0 4
3   111.7   112.5   180.0   10.6    8   0.2 1.0 5
3   111.9   112.5   180.0   10.6    8   0.2 1.0 6
3   112.3   112.5   180.0   10.6    8   0.4 2.0 7
3   112.4   112.5   180.0   10.6    8   0.2 1.0 8
4   112.7   113.6   172.8   7.6     6   0.1 0.8 2
4   112.9   113.6   172.8   7.6     6   0.2 0.9 3
4   113.0   113.6   172.8   7.6     6   0.1 0.8 4
4   113.2   113.6   172.8   7.6     6   0.2 1.0 5
4   113.4   113.6   172.8   7.6      6  0.2 1.1 6
5   115.5   116.8   187.5   9.2     8   0.2 1.0 2
5   115.6   116.8   187.5   9.2     8   0.2 0.9 3
5   115.8   116.8   187.5   9.2     8   0.2 0.9 4
5   116.0   116.8   187.5   9.2     8   0.2 1.0 5
5   116.2   116.8   187.5   9.2     8   0.2 1.0 6
5   116.6   116.8   187.5   9.2     8   0.4 2.0 7
5   116.8   116.8   187.5   9.2     8   0.2 1.0 8
6   117.1   118.2   160.0   8.9     6   0.2 1.0 2
6   117.2   118.2   160.0   8.9     6   0.1 0.9 3
6   117.4   118.2   160.0   8.9     6   0.1 0.9 4
6   117.5   118.2   160.0   8.9     6   0.2 1.0 5
6   117.7   118.2   160.0   8.9     6   0.2 1.1 6
7   121.8   123.6   125.0   5.4     8   0.2 1.2 2
7   122.1   123.6   125.0   5.4     8   0.4 2.9 3
7   122.3   123.6   125.0   5.4     8   0.1 1.0 4
7   122.5   123.6   125.0   5.4     8   0.3 2.2 5', header = T)

CodePudding user response:

If you want to do this in base R, you could try the following.

# Read in sample data as `df`

# Subset data 
plotdata <- df[df$p %in% c(2,4,7),]

#Create scatter plot
with(plotdata, plot(x = id, y = rat, col = p, pch = 20, bty = "n"))
legend("topleft", as.character(unique(plotdata$p)), col = unique(plotdata$p),
       pch = 20, bty = "n", title = "p")

enter image description here

  • Related