Reputation: 51
data:
V D1 D2 D3 CS1 CS2 CS3
10 2038 1806 1643 72.81171847 64.52304394 58.69953555
20 550 709 757 92.46159343 89.85351911 85.7449089
30 142 192 271 97.53483387 96.71311183 95.42693819
40 45 61 80 99.14255091 98.89246159 98.28510182
50 12 20 30 99.57127546 99.6070025 99.35691318
60 5 6 10 99.74991068 99.82136477 99.71418364
70 2 2 3 99.82136477 99.89281886 99.82136477
80 4 1 2 99.96427295 99.92854591 99.89281886
90 1 0 1 100.0000000 99.92854591 99.92854591
100 0 1 0 100.0000000 99.96427295 99.92854591
script:
mp<-barplot(as.matrix(t(df[,2:4])),beside=TRUE,xlim=c(0,40),ann=FALSE)
mp
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1.5 5.5 9.5 13.5 17.5 21.5 25.5 29.5 33.5 37.5
[2,] 2.5 6.5 10.5 14.5 18.5 22.5 26.5 30.5 34.5 38.5
[3,] 3.5 7.5 11.5 15.5 19.5 23.5 27.5 31.5 35.5 39.5
par(new=TRUE)
plot(mp[2,],df$CS1,xlim=c(0,40),type="l",col="red",axes=FALSE,ylim=c(0,100),ann=FALSE)
lines(mp[2,],df$CS2,col="blue")
lines(mp[2,],df$CS3,col="green")
axis(1,at=mp[2,],labels=df$V)
axis(4,at=seq(0,100,10))
creates this
If you look to the picture, the Y-axis are shifted to each other. How to make that the 0 of both Y-axis are on the same height?
Upvotes: 2
Views: 973
Reputation: 11128
I think you need to tweak par
in plot function as below,par(yaxs="i")
can help you in this case:
par(new=TRUE)
par(yaxs="i")
plot(mp[2,],df$CS1,xlim=c(0,40),type="l",col="red",axes=FALSE,ylim=c(0,100),ann=FALSE)
lines(mp[2,],df$CS2,col="blue")
lines(mp[2,],df$CS3,col="green")
axis(1,at=mp[2,],labels=df$V)
axis(4,at=seq(0,100,10))
?par from documentation:
Style "i" (internal) just finds an axis with pretty labels that fits within the original data range.
Output:
We can see that both the axes(y axis primary and secondary) have zeroes are on the same height from base or at same levels.
Upvotes: 0
Reputation: 42544
In his comment the OP stated
if you have a heavy barchart: it creates a visual strong 0 line. Adding line or dot graphs with a different y-scale which is not on the same height makes it visual difficult to read. In that case technically, a second 0 line should be drawn which is not on the same height
(Emphasis mine)
This is contrary to the OP's request to make that the 0 of both Y-axis are on the same height.
Facetting is an alternative approach. It avoids the visiual problems with two differently scaled y-axes in the same chart:
library(ggplot2)
ggplot(molten, aes(V, value, group = variable, colour = variable, fill = variable)) +
geom_col(data = molten[type == "D"], position="dodge") +
geom_line(data = molten[type == "CS"]) +
facet_wrap(~ type, scale = "free_y", ncol = 1L) + ylim(0, NA) +
theme_bw()
library(data.table)
DT <- fread(
"V D1 D2 D3 CS1 CS2 CS3
10 2038 1806 1643 72.81171847 64.52304394 58.69953555
20 550 709 757 92.46159343 89.85351911 85.7449089
30 142 192 271 97.53483387 96.71311183 95.42693819
40 45 61 80 99.14255091 98.89246159 98.28510182
50 12 20 30 99.57127546 99.6070025 99.35691318
60 5 6 10 99.74991068 99.82136477 99.71418364
70 2 2 3 99.82136477 99.89281886 99.82136477
80 4 1 2 99.96427295 99.92854591 99.89281886
90 1 0 1 100.0000000 99.92854591 99.92854591
100 0 1 0 100.0000000 99.96427295 99.92854591"
)
molten <- melt(DT, id.vars = "V")
molten[, type := stringr::str_extract(variable, "^\\D+")]
molten[, id := stringr::str_extract(variable, "\\d+$")]
Upvotes: 0
Reputation: 7153
Here's an alternate way to deal with data manipulation and visualisation using dplyr
and ggplot2
:
df <- df %>%
gather(D, d_val, D1:D3) %>%
gather(CS, cs_vsl, CS1:CS3)
This will provide a long format of the data-set, i.e.
head(df)
V D d_val CS cs_vsl
1 10 D1 2038 CS1 72.81172
2 20 D1 550 CS1 92.46159
3 30 D1 142 CS1 97.53483
4 40 D1 45 CS1 99.14255
5 50 D1 12 CS1 99.57128
6 60 D1 5 CS1 99.74991
With the long format a dual-axis plot can be done as so:
ggplot(df, aes(V)) +
geom_bar(aes(y=d_val, fill=D), stat="identity", position="dodge") +
geom_line(aes(y=cs_vsl*20, colour=CS)) +
scale_y_continuous(sec.axis = sec_axis(~./20, name = "CS")) + ylab("D") +
scale_x_continuous(breaks=seq(0,100, 10))
This method will resolve the issue of 0 on both axes not aligning on the same line.
Upvotes: 2