Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

r - 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?

question from:https://stackoverflow.com/questions/65865439/is-it-possible-to-have-x-axis-ticks-and-labels-for-no-data-points-in-ggplot-to-s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...