0

enter image description hereI am quite new to R and programming in general. So please forgive my ignorance, I am trying to learn.

I have two sets of data and I would like to plot them against each other. Both have 27 rows and 3 columns; one set is called "range" and the other is called "rangePx". Column “Comp” has the different components, column “Min” is the minimum concentration in % and column “Max” is the maximum concentration in %.

I want to make a 2-y axis dumbbell plot, with the y axis being the different components and x axis being the concentration.

I do manage to create 1 y axis dumbbell plot, but I have troubles to add the second y axis.

Here is a snap from the "range" data

 head(range)

# A tibble: 6 x 3
  Comp         Min    Max
  <chr>      <dbl>  <dbl>
1 Methane   0.0100 100   
2 Ethane    0.0100  65.0 
3 Ethene    0.100   20.0 
4 Propane   0.0100  40.0 
5 Propene   0.100    6.00
6 Propadien 0.0500   2.00

and here is a snap from the "rangePx" data

head(rangePx)
# A tibble: 6 x 3
  Comp           Min    Max
  <chr>        <dbl>  <dbl>
1 Methane   50.0     100   
2 Ethane     0.00800  14.0 
3 Ethene     0         0   
4 Propane    0.00800   8.00
5 Propene    0         0   
6 Propadien  0         0  

Here is the piece of code that I use:

library(ggplot2)
library(ggalt)
library(readxl)

theme_set(theme_classic())

range <- read_excel(range.xlsx)
rangePx <- read_excel(rangePx.xlsx")

p <- ggplot(range, aes(x=Max, xend=Min, y = Comp, group=Comp))
p <- p + geom_dumbbell(color="blue")
p
px <- ggplot(rangePx, aes(x=Max, xend=Min, y = Comp, group=Comp))
px <- px + geom_dumbbell(color="green")
p <- p + geom_dumbbell(aes(y=px, color="red"))
p

and here is the complain I get when I call p:

Error: Aesthetics must be either length 1 or the same as the data (27): y, colour, x, xend, group

Here I saw a 6x3 data frame but my original data are 27x3

can anyone help me?

Thnx in advance

Aloft
  • 1
  • 2
  • using facets is a more ggplot-ish strategy to show data on different scales http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/ – Nate Feb 23 '18 at 14:17
  • Thanks Nate, but I am not sure I understood your comment. The scales are not different, they all are from 0 to 100. I need to show my plot to a non R audience. A secondary y axis would make the plot quite straightforward to them. – Aloft Feb 23 '18 at 14:26
  • the last line `p <- p + geom_dumbbell(aes(y=px, color="red"))` makes no sense as `px` is a geom object not a column of data. – Stephen Henderson Feb 23 '18 at 14:35
  • FWIW `scale_y_continuous()` has a `sec.axis` parameter. Also: `dput(head(…))` output is far more useful than `head()` output (as the SO R FAQ states). – hrbrmstr Feb 23 '18 at 14:46
  • [SO R FAQ](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Nate Feb 23 '18 at 14:47
  • Why do you need secondary y-axis if `Comp` in `range` and `rangePx` are exactly the same? – pogibas Feb 23 '18 at 15:12
  • @PoGibas this is because I want to compare them in the same plot... – Aloft Feb 23 '18 at 18:00
  • @StephenHenderson thanks for your comment. Do you mean to replace y=px with y=rangePx? – Aloft Feb 23 '18 at 18:04
  • Why not to use different color, different shape of points or something else? – pogibas Feb 23 '18 at 18:33
  • @PoGibas it can be a solution and I will give it a shot. But sooner or later I will face this problem again and I would like to know the answer; not to mention the time I have invested so far :) – Aloft Feb 23 '18 at 19:31
  • I'm more than happy to help, but I can't image how those double y-axis would look with categorical data. Can you post a drawing? – pogibas Feb 23 '18 at 20:28
  • @PoGibas thanks, I have prepared a hand drawing, how can I send it to you? – Aloft Feb 27 '18 at 15:24
  • Just add it to the question – pogibas Feb 27 '18 at 15:27
  • @PoGibas It really struggles to upload the picture... My picture is 1.5 MB and the limit is 2 MB... Is it any other way to send it to you? – Aloft Feb 27 '18 at 15:45

1 Answers1

0

ggplot2 does not have the ability to plot 2 y-axes - this is an intentional decision by Hadley Wickham who wrote the package. You can see his response to a similar question here where he comments on his reasons for not including:

Plot with 2 y axes, one y axis on the left, and another y axis on the right

As mentioned in the comments and in reply to the question, if you want to use ggplot2 you have to use faceting to compare. Otherwise you need to use a different plotting package.

Andrew Haynes
  • 2,612
  • 2
  • 20
  • 35
  • 1
    Answer is wrong, see [this](http://ggplot2.tidyverse.org/reference/sec_axis.html) and [this](https://github.com/tidyverse/ggplot2/blob/4825054da690c181bbdfe733127b52d8c78c0488/R/axis-secondary.R) – pogibas Feb 23 '18 at 15:13
  • Answer is out of date, since version 2 2.2.0 it has been possible to add a secondary axis. – Jan Boyer Feb 23 '18 at 16:53