R ggplot:如何为连续的轴刻度创建条件标签 [英] R ggplot: How can I create conditional labeling for a continuous axis ticks

查看:69
本文介绍了R ggplot:如何为连续的轴刻度创建条件标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有条件地使用逻辑而不是硬编码来更改连续刻度标记的颜色/面部/等.例如:

I would like to conditionally alter the color/face/etc of a continuous tick mark label using logic instead of hard coding. For example:

library(tidyverse)
library(viridis)
xx=rpois(1000,lambda = 40)
y=density(xx,n=3600,from=0)

ggplot(data.frame(x = y$x, y = y$y), aes(x, y)) + 
  geom_line() + 
  geom_segment(aes(xend = x, yend = 0, colour = y)) + 
  scale_color_viridis() +
  labs(y='Density',x='Count',colour='Density')+
  geom_vline(xintercept=40,color='red') +
  scale_x_continuous(breaks=c(0,40,seq(25,100,25)),limits=c(0,100))+
  theme(axis.text.x = element_text(face=c('plain','bold',rep('plain',4)),
                                   color=ifelse(y$x==60,'red','black')))

因此,在上面的示例中,在 face 函数中可以看到硬编码,并且可以工作(我可以为 color 做同样的事情).这里没什么大不了的,因为我只有几个休息时间.但是,在将来的情况下,我可能需要更多的休息时间或我的轴所需的着色方案不太简单.有没有更有效的解决方案来基于条件逻辑创建此标签?我的第一次尝试是在 color 函数中看到的,但是没有用.问题是确定要在逻辑语句中使用的对象.也许我可以在幕后调用变量(类似于使用 .. level .. 进行密度图绘制).在这种情况下,如果您可以教我如何找到它/自己找出它,则可以加分

So in my example above, hard coding is seen in the face function and that works (I can do the same thing for color). It's not a big deal here since I only have a few breaks. In future scenarios, though, I may have significantly more breaks or a less simple coloring scheme needed for my axes. Is there a more efficient solution to create this labeling based on conditional logic? My first attempt is seen in color function but that does not work . The issue is identifying the object to use in the logical statement. Maybe there is a behind the scenes variable I can call for the breaks (similar to using ..level.. for a density plot). If that's the case, bonus points if you can teach me how to find it/figure that out on my own

推荐答案

感谢Djork教给我一些QnA礼仪...

Quick thanks to Djork for teaching me some QnA etiquette...

我能够通过在ggplot之外定义断点,然后在 theme 函数中使用 ifelse 逻辑来创建所需的结果来解决此问题.下面是我原始代码的更新(且有效)示例:

I was able to solve this problem by defining my break points outside of ggplot and then using ifelse logic within the theme function to create my desired outcome. The updated (and working) example of my original code is below:

library(tidyverse)
library(viridis)
xx=rpois(1000,lambda = 40)
y=density(xx,n=3600,from=0)

med_x=median(xx)
breakers = c(seq(0,100,25),med_x)

ggplot(data.frame(x = y$x, y = y$y), aes(x, y)) + 
  geom_line() + 
  geom_segment(aes(xend = x, yend = 0, colour = y)) + 
  scale_color_viridis() +
  labs(y='Density',x='Count',colour='Density')+
  geom_vline(xintercept=40,color='red') +
  scale_x_continuous(breaks=breakers,limits=c(0,100))+
  theme(axis.text.x = element_text(face=ifelse(breakers==med_x,'bold','plain'),
                                   color=ifelse(breakers==med_x,'red','black')))

我还没有在更复杂的逻辑上尝试过这种方法,但是我认为这种方法将适用于所有逻辑格式.

I haven't tried this on more complicated logic yet but I assume that this approach will work across all logical formatting.

这篇关于R ggplot:如何为连续的轴刻度创建条件标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆