Adding secondary y axis in ggplot2 without scaling factor
0
0
Entering edit mode
20 months ago
StartR ▴ 30

HI

I want to plot two y axis - one with continuous data and other with values - ranging from 0 to 7

Example:

ID IHC   FISH1  FISH2              
1   3    11.5   9.5
2   1    2.9    3.9
3   2    1.5    6.5
4   1    3.3    1.3
5   2    5.5    8.5
6   2    6.6    9.6

How can I plot secondary y axis - if it is not related to primary y axis

enter image description here

I want to code in R.

I want a plot like this as output

This is my code:

data %>% 
  select(ID, IHC, FISH1, FISH2) %>% 
  gather(key = "FISH_IHC", value = "FISH_val", FISH1, FISH 2, 
         IHC, -ID) %>% 
  mutate(as_factor = as.factor(FISH_IHC)) %>% 
  ggplot(aes(x = reorder(ID,FISH_val), y = FISH_val, group = as_factor), na.rm = TRUE) +
  geom_point(aes(shape=as_factor, color=as_factor)) +
  scale_y_continuous(limits = c(0, 10),
                     oob = function(x, ...){x},
                     expand = c(0, -1),
                     breaks=number_ticks(10),
                    sec.axis = sec_axis(scales::rescale(pretty(range(IHC)), 
                                                        name = "IHC"))
ggplot • 375 views
ADD COMMENT

Login before adding your answer.

Traffic: 1988 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6