功能sec.axis不显示第二个变量的条形
我必须计算一个带有2个bar的图,每个X观测值。 Y1是一个可以假设大量值的变量(也数十万) Y2在[0,100]范围内。 我报告了一个有虚拟数据的示例。 问题是Y1还可以,但是参考第一个轴值而不是第二个轴(因此,Y2是看不见的)。
library(tidyverse)
data = data.frame("x" = c("A", "B", "C", "D", "E", "F", "G", "H"),
"y1" = c(10000, 70000, 120000, 75000, 500, 2000, 10000, 18000),
"y2" = c(22, 30, 36, 10, 1, 80, 33, 39))
data %>%
gather("Type", "Value", -x) %>%
ggplot(aes(x, Value, fill = Type))+
geom_bar(position = "dodge", stat = "identity")+
scale_y_continuous(name = "First axis",
sec.axis = sec_axis(~./1000, name = "Second axis"))
此外,将Y2轴限制为100是很好的。
I have to compute a plot with 2 bars for each x observation.
y1 is a variable that can assume large values (also hundreds of thousands)
y2 is in the range [0, 100].
I have reported an example with fictitious data.
The problem is that y1 is okay, but y2 is plotted with reference to the first axis values instead of the second one (and so it's pratically invisible).
library(tidyverse)
data = data.frame("x" = c("A", "B", "C", "D", "E", "F", "G", "H"),
"y1" = c(10000, 70000, 120000, 75000, 500, 2000, 10000, 18000),
"y2" = c(22, 30, 36, 10, 1, 80, 33, 39))
data %>%
gather("Type", "Value", -x) %>%
ggplot(aes(x, Value, fill = Type))+
geom_bar(position = "dodge", stat = "identity")+
scale_y_continuous(name = "First axis",
sec.axis = sec_axis(~./1000, name = "Second axis"))
Moreover, it would be good limiting the y2 axis to 100.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在
sec_axis
中应用转换就不够。您还必须重新列出我添加突变
步骤的数据。要获得辅助轴的最大范围,您必须调整缩放率,即您的示例数据1250
工作。Simply applying a transformation in
sec_axis
isn't sufficient. You also have to rescale the data for which I added amutate
step. To get a max range for the secondary axis you have to adjust the scaling, i.e. for your example data1250
works.