1
votes

I have 4 data-frames storing a number (between 1 and 100) and a string, which can be either 'A', 'B', or 'C'. I want to create a scatter-plot with the x axis in the order 'A', 'B', 'C', with the numbers of each data-frame on the y-axis. The dots on the scatter-plot should be different for each of the data-frames.

To give you some idea, the data-frames are in the form

Name | Number

 A   |   2
 A   |   4
 B   |   5

I've tried the following, but for some reason, it adds the numbers instead of plotting them as is.

Note: class.df is the original data-frame from which df1, df2, and df3 are based on. I don't want to use it, if possible.

p <- plot_ly(class.df, x = ~Name, type = 'scatter') %>%
add_trace(df1, y = ~Number, x = ~Name, mode = 'markers', name = '1') %>%
add_trace(df2, y = ~Number, x = ~Name, mode = 'markers', name = '2') %>%
add_trace(df3, y = ~Number, x = ~Name, mode = 'markers', name = '3')
1

1 Answers

0
votes

Hi I´am also new but hopefully this can help you if I understand you right. I have created three separate dataframes. For these dataframes I have added a additional column with a specific number for that dataframe. In the ggplot command i distinguish the colours with the col argument. Finally I used ggplotly to do plotlys lovely stuff. I hope i have answered your question properly.

Create 100 random numbers between 1 - 100
Number <- sample(100, 100)
Number2 <- sample(100, 100)
Number3 <- sample(100, 100)
Letters A B end C
Letters <- c("A", "B", "C")
Get 100 times random letter A, B or C
Letter <- sample(Letters, 100, replace = T)
Letter2 <- sample(Letters, 100, replace = T)
Letter3 <- sample(Letters, 100, replace = T)

Create three dataframes
dat1 <- data.frame("Letter" = Letter, "Number" = Number)
dat2 <- data.frame("Letter" = Letter2, "Number" = Number2)
dat3 <- data.frame("Letter" = Letter3, "Number" = Number3)

Colour for each separate dataframe
dat1$Colour <- "1"
dat2$Colour <- "2"
dat3$Colour <- "3"

Bind all dataframes
datall <- rbind(dat1,dat2,dat3)
Create a ggplot x = Letter, y = Number, col = Seperate colour for each dataframe
A <- ggplot(data=datall, aes(x = Letter, y = Number, col = Colour)) + geom_point() + theme_classic()
Ggplotly to do the magic
ggplotly(A)