0

I have three columns in my data. Name, TP and FP and I want to plot a Cleveland dot plot which is ordered by the value of TP (in descending order) showing BOTH the TP and FP value for the respective Name.

R code

data <- read.csv("averages-names.csv")

data <- data %>% group_by(Name) %>% summarise(TP = sum(TP, na.rm=TRUE), FP = sum(FP, na.rm=TRUE)) %>% arrange(TP) %>% mutate(Name = factor(Name, levels = .$Name))

p = ggplot(data, aes(TP, Name)) + geom_point(color = "blue")
q = ggplot(data, aes(FP, Name)) + geom_point(color = "red")

This shows them successfully on two seperate graphs, but I want to plot both columns on the same graph.

my data looks like this:

data
A tibble: 173 x 3
   Name                                          TP    FP
   <fct>                                      <dbl> <dbl>
 1 Audi S5 Coupe 2012                         0.214 0.633
 2 Chevrolet Express Cargo Van 2007           0.267 0.361
 3 Audi 100 Wagon 1994                        0.317 0.663
 4 Chevrolet Silverado 1500 Extended Cab 2012 0.422 0.633

2 Answers2

0

This becomes a lot easier if you reshape your data as follows:

df <-
  tibble(
    car = c("audi", "chevy", "bmw", "mercedes"),
    TP = c(0.633, 0.361, 0.663, 0.633),
    FP = c(0.214, 0.267, 0.317, 0.422)
  )

df_rev <-
  df %>% 
  gather(key = metric, value = value, TP:FP)

> df_rev
# A tibble: 8 x 3
  car      metric value
  <chr>    <chr>  <dbl>
1 audi     TP     0.633
2 chevy    TP     0.361
3 bmw      TP     0.663
4 mercedes TP     0.633
5 audi     FP     0.214
6 chevy    FP     0.267
7 bmw      FP     0.317
8 mercedes FP     0.422

Then you can do a much more minimal call for the plot:

df_rev %>% 
  ggplot(aes(x = value, y = car, color = metric)) +
  geom_point()
cardinal40
  • 1,245
  • 1
  • 9
  • 11
0

Or if you just want to keep your data as it is.

data <- tibble::tribble(
                                         ~Name,   ~TP,    ~FP,
                          "Audi S5 Coupe 2012", 0.214,  0.633,
            "Chevrolet Express Cargo Van 2007", 0.267,  0.631,
                         "Audi 100 Wagon 1994", 0.317,  0.663,
  "Chevrolet Silverado 1500 Extended Cab 2012", 0.422, 0.6633
  )

library(ggplot2)

ggplot(data ) + 
  geom_point(aes(TP, Name, color = "blue")) + 
  geom_point(aes(FP, Name, color = "red")) +
  labs(x = "metric")
Ryan John
  • 1,410
  • 1
  • 15
  • 23