I have a df:
Sample_ID = c("LSL Guideline", "USL Guideline", "P1014B", "P1014F", "P1036A", "P1036B", P1036C","P1036D" ,"P1036E, "P1036F")
CONTAMINATION_SCORE (NA) = c(0, 3106, 2677, 1021, 870, 6831, 1324, 4175, 1370, 875)
CONTAMINATION_P_VALUE (NA) = c(0.000, 0.049, 0.101, 1.000, 1.000, 0.000, 1.000, 0.036, 1.000, 1.000)
df <- data.frame(Sample_ID, CONTAMINATION_SCORE (NA), CONTAMINATION_P_VALUE (NA) )
> df
Sample_ID CONTAMINATION_SCORE..NA. CONTAMINATION_P_VALUE..NA.
1 LSL Guideline 0 0.000
2 USL Guideline 3106 0.049
3 P1014B 2677 0.101
4 P1014F 1021 1.000
5 P1036A 870 1.000
6 P1036B 6831 0.000
7 P1036C 1324 1.000
8 P1036D 4175 0.036
9 P1036E 1370 1.000
10 P1036F 875 1.000
I am trying to follow the guide here:
Combine bar and line chart in ggplot2
I want to plot all but the first 2 rows of df and have the following code.
It nearly works as the guide says but the second axis isn't working?? E.g. I want to see the CONTAMINATION_P_VALUE (NA)
column as the line on the second axis as the guide shows
ggplot(df[-c(1,2),]) +
geom_bar(aes(x=Sample_ID, y=`CONTAMINATION_SCORE (NA)`),stat="identity", fill=rainbow(n=length(df$Sample_ID[-c(1:2)])))+
geom_line(aes(x=Sample_ID, y=`CONTAMINATION_P_VALUE (NA)`),stat="identity",color="red")+
labs(title= " QC",
x="Sample ID",y=" Score") +
scale_y_continuous(sec.axis=sec_axis(~.*0.01,name="Percentage"))