简体   繁体   中英

Is it possible to have x axis ticks and labels for no data points in ggplot to show only one x-axis result?

I've got a data set of

Target_num <- c(40, 40, 40, 40, 40, 40, 120, 120, 120, 120, 120, 120, 160, 160, 160, 160, 160,160,160, 200, 200, 200, 200, 200, 200, 240, 240, 240, 240, 240)
BP <- c(1:30)
db_s_abs <- data.frame(Target_num,BP)

And I'm trying to show only the data set for Target_num==160 but with the x-axis marks still present for 40-320 by 40.

a <- db_s_abs %>%
  filter(Type=="BP") %>%
  filter(Target_num=="160")

#Factoring column

db_s_abs$Target_num <- factor(db_s_abs$Target_num, levels=c("40","80","120","160","200","240", "280", "320"))

#Plotting chart
p_optim_bp_abs <- ggplot(aes(x=Target_num, y=SBP_Delta), data=db_s_abs, color="black")+
  geom_hline(yintercept=0, col='grey')+
  geom_point(aes(x=a$Target_num), alpha=0.8, position=position_dodge(width=1), color="blue", data=a)+
  geom_smooth(method="matt", formula=y~x, size=1.5, se=TRUE, alpha=0.5, aes(x=a$Target_num, y=a$SBP_Delta), data=a)+
  stat_summary(geom='pointrange', fun.data="mean_cl_boot", position=position_dodge(width=1), size=0.6, data=a)+
  theme_minimal()+
  scale_x_discrete(name="AV Delay", breaks=seq(40,320,40))+
  scale_y_continuous(name="Change in BP (mmHg)", breaks = seq(-10,10, 2.5))

p_optim_bp_abs

I've tried changing it to

 scale_x_discrete(name="AV Delay", breaks=db_s_abs$Target_num)

and changing the filter to a, taking out the filter, changing the breaks to a factored vector, but in spite of having factored the vector, it either comes up out of order with the stat_summary lines for all the other Target_num or with just 160 on the x-axis with the data I want it to show.

Any ideas how I can get it to show 40, 120, 160, 200, 240, 280, 320 on the x-axis and only display data points for 160?

I assume you want something like this?

library(dplyr)
library(ggplot2)
Target_num <- c(40, 40, 40, 40, 40, 40, 120, 120, 120, 120, 120, 120, 
                160, 160, 160, 160, 160,160, 160, 
                200, 200, 200, 200, 200, 200, 240, 240, 240, 240, 240)
db_s <- data.frame(Target_num, BP=1:30)
ggplot(aes(x=Target_num, y=BP), data=db_s) +
    geom_hline(yintercept=0, col="grey") +
    scale_x_continuous(name="AV Delay", labels=seq(40, 320, by=40), 
                     breaks=seq(40, 320, by=40), limits = c(40, 320)) +
    geom_smooth(method="lm", formula=y~x, size=1.5, se=TRUE, alpha=0.5) +
    stat_summary(data=subset(db_s, Target_num==160), geom="pointrange", 
                 fun.data="mean_cl_boot", size=0.6) +
    geom_point(data=subset(db_s, Target_num==160), alpha=0.8, color="blue") +
    scale_y_continuous(name="Change in BP (mmHg)", breaks = seq(-10, 10, 2.5),
                       limits=range(c(seq(-10, 10, 2.5), db_s$BP))) +
    theme_minimal()

Created on 2021-01-24 by the reprex package (v0.3.0)

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