How to plot 3 single numbers from multiple columns in a bar graph using ggplot2?

I have a data frame with 3 specific columns which I want to do a plot from in ggplot2. My question involves two different topics actually, and I'd be glad if you guys answer any of the two even if you don't know how to do the other one.

So, basically, the columns of the data frame are like this:

gp1_percnt | gp2_percnt | gp3_percnt 
    0.65         0.50         0.45
    ...          ...          ...

    ...          ...          ...

    ...          ...          ... 
    0.80         0.20         0.40

The thing is, I want to plot the last line of each column, on the same graph, every 5 minutes in my Shiny application. So the problem involves unifying 3 columns on the same graph, and plotting a single number with a proportional bar chart.

For the last line in the example - 0.80 0.20 0.40 - I'd expect to get something like this:

         Percentual of GP used
         -1.0
         -.80  ###            # GP1
         -.60  ###            @ GP2
         -.40  ###       $$$  $ GP3
         -.20  ###  @@@  $$$
               GP1  GP2  GP3

(If you know how to fix the y axis from 0.0 to 1.0, as it represents a percentual, that would be a bonus!)

Upvotes: 0

Views: 99

Answers (1)

Jon Spring
Jon Spring

Reputation: 66415

library(tidyverse)
my_data %>%                        # Start with data,
  slice(n()) %>%                   # grab last row
  gather(column, value) %>%        # reshape to long format
  ggplot(aes(column, value)) +     # map "column" to x, "value" to y 
  geom_col() +                     # bar chart (equiv to geom_bar(stat="identity")
  scale_y_continuous(name = "Percentual of GP used") +
  coord_cartesian(ylim = c(0,1))   # set y range from 0 to 1

enter image description here

Upvotes: 2

Related Questions