简体   繁体   English

是否可以在 ggplot 中为没有数据点的 x 轴刻度和标签以仅显示一个 x 轴结果?

[英]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.而且我试图仅显示 Target_num==160 的数据集,但 x 轴标记仍然存在 40-320 x 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.并将过滤器更改为a,取出过滤器,将中断更改为因式向量,但尽管已对向量进行因式分解,但它要么与所有其他 Target_num 的 stat_summary 行乱序,要么仅打开 160 x 轴与我希望它显示的数据。

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?有什么想法可以让它在 x 轴上显示 40、120、160、200、240、280、320 并且只显示 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)代表 package (v0.3.0) 于 2021 年 1 月 24 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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