-1

I was searching for a code to create a nice graph with two y axes. However i couldn't find a solution which helped me. (I am not using r often)

I have the time as x variable where i want to plot value1 and value2 on the right and left y achses respectively. My data are in one dataset, so that i want to graph datasat$time on x axis and data$value1 on y left axis and data$value2 on right y axis. The way to illustrate should be a line with two different colours for each (data$value1 and data$value2). In addition there should be a legend for the lines.

Can someone help me with it?

mrhellmann
  • 5,069
  • 11
  • 38
Dam
  • 1
  • There are several way to achieve this using either `ggplot2` or `plotly`. You need to provide example data and possibly some of your code. – Zhiqiang Wang Jan 08 '20 at 12:02
  • 2
    Does this answer your question? [How can I plot with 2 different y-axes?](https://stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes) – Miff Jan 08 '20 at 12:34
  • 1
    I recently posted a script incl data here https://stackoverflow.com/questions/59628200/how-can-i-synchronize-the-bar-width-between-two-histograms-when-merging-with-gga – cmirian Jan 08 '20 at 12:51

1 Answers1

0

Please, find my data w below.

Here is a script on how a dobbelt y-axis may be integrated using ggplot

 ggplot()  +
  geom_bar(mapping = aes(x = w$WHO[w$Death==1]), 
 stat = "count", alpha=0.2, colour="#1C73C2", 
  fill="#ECF0F9")  +

  scale_y_continuous(name = "Number of deaths", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "Mortality rate per 100 person-yrs", breaks=yaks, labels=c("0","5","10","15","20","25","30","35","40","45"))) +
  scale_x_continuous(name="", breaks = c(1,2,3,4), labels =c("\nWHO-I\nn=37","\nWHO-II\nn=29","\nWHO-III\nn=19","\nUnknown\nn=25")) +

  coord_cartesian(ylim=c(0, 18)) +

  geom_point(mapping = aes(x = 1, y = 3.329993), size=5,alpha=0.7, shape=18, colour="red") +
  geom_point(mapping = aes(x = 2, y = 12.424504), size=5,alpha=0.7, shape=18, colour="red") +
  geom_point(mapping = aes(x = 3, y = 17.23519), size=5, alpha=0.7,shape=18, colour="red") +
  geom_point(mapping = aes(x = 4, y = 4.549763), size=5, alpha=0.7, shape=18, colour="red") +

  annotate("text", x = c(1,2,3,4) , y = c(3.329993+1.3, 12.424504+1.3, 17.23519+1.3,4.549763+1.3 ),  label = c("8.3","31.1","43.1","11.4"), col="red", fontface=2, cex=4) +

  theme(axis.text.y.right = element_text(color = "red", size = 11), 
        axis.title.y=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 15, b = 0, l = 0)), 
        axis.text.y = element_text(color = "#1C73C2", size = 11), 
        axis.title.y.right=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 0, b = 0, l = 15)),
        axis.text.x = element_text(color = "grey20", size = 11))

My data w

w <- structure(list(WHO = c(1L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 
3L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 4L, 4L, 1L, 4L, 1L, 2L, 1L, 4L, 1L, 4L, 4L, 4L, 
4L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 2L, 1L, 2L, 2L, 4L, 4L, 
4L, 2L, 4L, 1L, 4L, 4L, 2L, 4L, 4L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 
3L, 4L, 3L, 4L, 3L), response = c(0L, 1L, 0L, 0L, 0L, 1L, 1L, 
1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0L, 0L, 
0L, 0L, 0L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