Home > database >  Plotting multiple recorded values in R
Plotting multiple recorded values in R

Time:11-20

I have a csv file that looks like this:

Specialty, Recording 1, Recording 2, Recording 3
A,          100%,         200%,     300%
B,          50%,         -75%,      150%
C,          60%,          100%,    -25%

I would like to plot them on a graph so that it looks like this: text

I believe I can do so with this:

ggplot(data2, aes(x=Recording 1, y=Specialty))   geom_point()

How do I add Recording 2 and 3 to the same graph?

CodePudding user response:

If that is example what the data looks like, I recreated it here for my answer.

Example data

structure(list(Specialty = structure(1:3, .Label = c("A", "B", 
"C"), class = "factor"), Recording.1 = structure(1:3, .Label = c("100%", 
"50%", "60%"), class = "factor"), Recording.2 = structure(c(3L, 
1L, 2L), .Label = c("-75%", "100%", "200%"), class = "factor"), 
    Recording.3 = structure(3:1, .Label = c("-25%", "150%", "300%"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-3L))

First, since the data has %'s you will need to remove that. ggplot needs numeric values to plot. NOTE: since you columns titles have spaces in them they need to be in

df$`Recording 1` <- as.numeric(sub("%", "", df$`Recording 1`))
df$`Recording 2` <- as.numeric(sub("%", "", df$`Recording 2`))
df$`Recording 3` <- as.numeric(sub("%", "", df$`Recording 3`))

library(ggplot2)
library(reshape2)

You can then use reshape2 so you can plot everyone together.

df <- melt(df, id.vars='Specialty')

After that, you should be good to go to make the plot

ggplot(df, aes(x=value, y=Specialty, fill=variable, color=variable))   geom_point(stat='identity')   scale_x_continuous(name = "whatever (%)", limit = c(-100, 300))

or to add percent in the x-axis

library(scales)
ggplot(df, aes(x=value, y=Specialty, fill=variable, color=variable))   geom_point(stat='identity')   scale_x_continuous(labels = percent_format(scale = 1), name = "whatever (%)", limit = c(-100, 300))

Here is the output

example_update

With percents in x-axis

example_update2

CodePudding user response:

I've made you an example data set. I've assumed you don't have the "%" in the dataset and the fields are numeric. We add the "%" to the x-axis label using the scales package. Point here is to first make your data long with pivot_longer.

library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)

df <- data.frame(Specialty = c("A", "B", "C"),
                 Recording_1 = c(100, 50, 60),
                 Recording_2 = c(200, -75, 100),
                 Recording_3 = c(300, 150, -25))


df %>% 
    pivot_longer(cols = !Specialty, names_to = "Recording",
                    values_to = "Value") %>%
    ggplot(aes(x = Value, y = Specialty))  
        geom_point(aes(shape = Recording, color = Recording), size = 3) 
    scale_y_discrete(limits = rev)   #reverses A,B,C order
    scale_x_continuous(labels = percent_format(scale = 1))  
    theme_classic()

Here is what the output looks like. enter image description here

  •  Tags:  
  • r
  • Related