简体   繁体   中英

Why are the segments not plotting correctly?

So, I have this code:

HealthyD1 <-  1
HealthyD2 <- 1.02739726
HealthyD4 <- 1.01369863

IllD1 <- 1
IllD2 <- 1.28
IllD4 <- 1.48


barplot(c(HealthyD1, HealthyD2, HealthyD4, IllD1, Ill3D2, IllD4), 
        ylim = c(0, 1.6), names.arg = rep(c("D1", "D2", "D4"), 2),
        ylab = "Number", main = "Number of patients",
        col = c("lightskyblue1", "lightskyblue3", "lightskyblue4",
                "plum1", "plum", "plum4"), xpd = FALSE)


segments(x0 = 0, y0 = 0, x1 = 7, lwd = 1.4)
segments(x0 = 0.22, y0 = -0.3, x1 = 3.6, lwd = 1.4)
segments(x0 = 3.83, y0 = -0.35, x1 = 7.15, lwd = 1.4)

axis(1, at = 1.99, labels = "Healthy", pos = -0.2, tick = FALSE)
axis(1, at = 5.55, labels = "Ill", pos = -0.2, tick = FALSE)

I want to plot the data using a barplot, as you can see. I also want to draw a couple of segments just above the "Healthy" and "Ill". I ran this code a couple of days ago and it worked as I wanted to, but now, these segments don't appear. Why is this happening? How can I fix it? Thanks in advance!

You are trying to plot below the bar,i guess you need to set xpd=TRUE ?

barplot(c(HealthyD1, HealthyD2, HealthyD4, IllD1, IllD2, IllD4), 
        ylim = c(0, 1.6), names.arg = rep(c("D1", "D2", "D4"), 2),
        ylab = "Number", main = "Number of patients",
        col = c("lightskyblue1", "lightskyblue3", "lightskyblue4",
                "plum1", "plum", "plum4"), xpd = FALSE)


segments(x0 = 0, y0 = 0, x1 = 7, lwd = 1.4,xpd=TRUE)
segments(x0 = 0.22, y0 = -0.3, x1 = 3.6, lwd = 1.4,,xpd=TRUE)
segments(x0 = 3.83, y0 = -0.35, x1 = 7.15, lwd = 1.4,,xpd=TRUE)

axis(1, at = 1.99, labels = "Healthy", pos = -0.2, tick = FALSE)
axis(1, at = 5.55, labels = "Ill", pos = -0.2, tick = FALSE)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM