0

Hello I am trying to do scatter plot using R, new to R. example graph which I wanted looking as below imageenter image description here

How can I obtaine this type of graph in R anyone suggest me. my dataframe which is looking like as below

data = {'grp1': [1,1,1,1,1,1,1,1,1,0.7], 'grp2': [1,1,1,1,1,0.7,0.9,0.8,0.9,1,1,1]}

df = pd.DataFrame(data)

enter link description here this one I tried to solve but not able do

Thank you

1 Answers1

1

We can reshape to 'long' format and create a boxplot

library(dplyr)
df %>%
     pivot_longer(cols = everything()) %>%
     ggplot(aes(x = name, y = value)) + 
          geom_boxplot()

Or a dot plot

df %>% 
    pivot_longer(cols = everything()) %>% 
    ggplot(aes(x = name, y = value, fill = name)) + 
      geom_dotplot(binaxis = 'y', stackdir = 'center', 
           position = position_dodge(), dotsize = 0.5) +
      theme_bw()

enter image description here

data

df <- structure(list(grp1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 0.7), grp2 = c(1, 
1, 1, 1, 1, 0.7, 0.9, 0.8, 0.9, 1)), class = "data.frame", row.names = c(NA, 
-10L))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for reply.In one of my group NA that values are not present in another group in that case how can run above answer – Hemantchandru naik Jul 21 '20 at 23:14
  • @Hemantchandrunaik Not clear. Your example showed was in python and the lengths were not same for each column. Can you show the `dput` for example in `R` – akrun Jul 21 '20 at 23:15
  • okay sorry for python code, I re created daframe `data = c("grp1","grp1","grp1","grp1","grp1","grp1","grp1","grp1","grp1","grp2","grp2","grp2","grp2","grp2","grp2","grp2") values=c(1,1,1,1,0.7,1,1,1,1,1,1,0.9,0.8,1,0.5,1) df = data.frame(data,values)` – Hemantchandru naik Jul 21 '20 at 23:35
  • for above dataframe can graph can fit? – Hemantchandru naik Jul 21 '20 at 23:39
  • @Hemantchandrunaik With your new 'df'. `ggplot(df, aes(x= data, y = values, fill = data)) + geom_dotplot(binaxis = 'y', stackdir = 'center', position = position_dodge(), dotsize = 0.5) + theme_bw()` – akrun Jul 21 '20 at 23:45
  • just asking for option in ggplot like header for a graph and I wanted hide the background bins and x axis line. is there any option in ggplot – Hemantchandru naik Jul 21 '20 at 23:56
  • @Hemantchandrunaik for the title, you can use `+ ggtitle('scatterplot')` – akrun Jul 21 '20 at 23:59
  • @Hemantchandrunaik you can do `ggplot(df, aes(x= data, y = values, fill = data)) + geom_dotplot(binaxis = 'y', stackdir = 'center', position = position_dodge(), dotsize = 0.5) + theme_bw() + ggtitle('scatterplot') + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())` – akrun Jul 22 '20 at 00:00