R GGPLOT从一个数据框中的数据中绘制,使用数据框架标签轴ticks ticks ticks ticks ticks tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick?

发布于 2025-02-13 23:43:21 字数 1252 浏览 5 评论 0原文

我有2个数据集。在data中,b被编码为数字值。 data1表示b是chr data的原始内容。

我想使用数据的数字数据绘制一个框图,但是从b in data1中拉出y轴tick标签。 。这可能吗?

玩具数据:

data <- data.frame(a = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
                   b = c(10,10,11,11,12,12,12,13,13,13,14,14,14,15,15),
                   c = c('x','y','y','x','y','y','x','y','y','x','y','y','x','x','y'))

data1 <- data.frame(a = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
                   b = c('never','never','rare','rare','some','some','some','most','most','most','often','often','often','always','always'),
                   c = c('x','y','y','x','y','y','x','y','y','x','y','y','x','x','y'))

我尝试使用scale_y_discrete

ggplot(data,aes(x = as.factor(a), y = b, fill = c))+
      geom_boxplot()+
  scale_y_discrete(data1$b,labels=unique(data1$b))

“在此处输入图像说明”

作为您可以看出,Y轴tick标签未按预期显示。

为了进一步参考,这将用于更大的数据集,作为闪亮应用程序的一部分。该图形成了一个反应元素,因此我需要避免对实际标签名称进行硬编码。

感谢您的帮助。

I have 2 data sets. In data, b is encoded as numeric values. data1 represents the original in which b is chr data.

I would like to plot a box plot using the numeric data from data but pull the y-axis tick labels from the chr data b in data1. Is this possible?

Toy data:

data <- data.frame(a = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
                   b = c(10,10,11,11,12,12,12,13,13,13,14,14,14,15,15),
                   c = c('x','y','y','x','y','y','x','y','y','x','y','y','x','x','y'))

data1 <- data.frame(a = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
                   b = c('never','never','rare','rare','some','some','some','most','most','most','often','often','often','always','always'),
                   c = c('x','y','y','x','y','y','x','y','y','x','y','y','x','x','y'))

My attempt to achieve the outcome using scale_y_discrete:

ggplot(data,aes(x = as.factor(a), y = b, fill = c))+
      geom_boxplot()+
  scale_y_discrete(data1$b,labels=unique(data1$b))

enter image description here

As you can see, the y-axis tick labels are not displaying as anticipated.

For further reference, this is to be used on a far larger data set as part of a Shiny application. The plot forms a reactive element and so I need to avoid hard-coding the actual label names.

Your help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追风人 2025-02-20 23:43:22

由于您的b变量是连续的,因此必须使用scale_y_continous。对于您的示例数据scale_y_continouul(labels = unique(data1 $ b))会起作用,我个人建议使用一个命名的标签矢量,该标签较少,并且将标签分配给数字值:

library(ggplot2)

labels <- unique(data1$b)
names(labels) <- 10:15

ggplot(data,aes(x = as.factor(a), y = b, fill = c))+
  geom_boxplot()+
  scale_y_continuous(labels = labels)

< img src =“ https://i.sstatic.net/vzq5c.png” alt =“”>

As your b variable is continuous you have to use scale_y_continuous. While for your example data scale_y_continuous(labels = unique(data1$b)) will work, personally I would suggest to use a named vector of labels which is less error prone and assigns labels to numeric values:

library(ggplot2)

labels <- unique(data1$b)
names(labels) <- 10:15

ggplot(data,aes(x = as.factor(a), y = b, fill = c))+
  geom_boxplot()+
  scale_y_continuous(labels = labels)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文