0

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!)

2
  • 1
    Did you draw that bar plot by typing? If it is the case, that bar plot with the characters is just amazing ! :) I liked your creativity! Seriously. Commented Aug 8, 2019 at 17:21
  • @maydin Yes! thank you! hahahaha Commented Aug 9, 2019 at 4:32

1 Answer 1

2
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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I didn't know gather could take 'column' and 'value' as parameters!
You could really use any two words there, I just thought those were useful names to use for the two first parameters of gather, which correspond to the "key" and "value," respectively. In other words, the code is short for gather(key = column, value = value).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.