R 具有两个Y轴的分组箱线图

R 具有两个Y轴的分组箱线图,r,ggplot2,graphics,boxplot,R,Ggplot2,Graphics,Boxplot,我想使用GGPLOT2为同一类别创建两个具有不同信息的箱线图,两个Y轴。 如下图所示: 我有两个区域(AM和AR)的类别信息(速度和旋转角度),我想将这两种运动模式组合在一个根据区域分组的图形中 到目前为止,我的代码如下所示: areaveleang <- ggplot(dados2, aes(x = area)) + geom_boxplot(aes(y = speed, color="Bruto" ), fill = "darkblue&

我想使用GGPLOT2为同一类别创建两个具有不同信息的箱线图,两个Y轴。 如下图所示:

我有两个区域(AM和AR)的类别信息(速度和旋转角度),我想将这两种运动模式组合在一个根据区域分组的图形中

到目前为止,我的代码如下所示:

areaveleang <- ggplot(dados2, aes(x = area)) +
        geom_boxplot(aes(y = speed, color="Bruto" ), fill = "darkblue", alpha=0.7) +
  geom_boxplot(aes(y = angulo_modulo), color="#a50026", position = "dodge") +
        scale_y_continuous(name = "speed (km/h)", sec.axis = sec_axis(~ . *6,name=expression("turn angle (graus)"))) +
                           #breaks = seq(0, 175, 25),
                           #limits=c(0, 175)) +
        scale_x_discrete(name = "Area") +
        #ggtitle("Boxplot of mean ozone by month") +
        theme_bw() +
        theme(plot.title = element_text(size = 14, family = "Tahoma", face = "bold"),
              text = element_text(size = 12, family = "Tahoma"),
              axis.title = element_text(face="bold"),
              axis.text.x=element_text(size = 11),
              legend.position = "bottom") +
        scale_fill_brewer(palette = "Accent")

PS:对不起,我的英语很差,我希望你能理解我的问题xD

不确定我是否正确,但也许这就是你想要的:

  • 要获得次轴的正确比例,您必须对数据应用逆变换,即当您的值乘以6时,您必须将数据中的
    angulo_模
    除以6

  • 要避开两个箱线图,我建议使用
    tidyr::pivot\u longer
    ,使两个变量成为一个变量的两个类别,默认情况下命名为
    name
    ,而值转到
    value
    。通过这种方式,您可以在
    y
    上映射
    value
    ,在
    color
    上映射
    name
    alpha
    fill
    。默认情况下,
    ggplot2
    将为每个区域提供两个并排的方框图

  • 库(ggplot2)
    图书馆(tidyr)
    图书馆(dplyr)
    数据量2%>%
    突变(安古洛模=安古洛模/6)%>%
    枢轴长度(cols=c(速度,角度模数))%>%
    突变(名称=因子(名称,等级=c(“速度”,“安格鲁-模数”))%>%
    ggplot(aes(x=面积))+
    geom_箱线图(aes(y=值,颜色=名称,alpha=名称,填充=名称))+
    缩放连续(name=“速度(km/h)”,秒轴=秒轴(~*6,name=表达式(“转角(graus)”))+
    比例x离散(name=“Area”)+
    比例颜色手动(值=c(angulo_modulo=“暗蓝色”,speed=“#a50026”))+
    比例α手册(值=c(0.7,1))+
    主题_bw()+
    主题(plot.title=element\u text(大小=14,family=“sans”,face=“bold”),
    text=element\u text(大小=12,family=“sans”),
    axis.title=元素\文本(face=“bold”),
    axis.text.x=元素\文本(大小=11),
    legend.position=“底部”)+
    缩放填充酿酒器(调色板=“重音”)
    

    嘿,安妮,我想澄清一下,ggplot2在生成两个y轴方面非常有限。所以,如果你真的需要两个y轴,你将有一个困难的时间。更多信息请参阅本文:这正是我所需要的!谢谢@Stefan
    structure(list(lon = c(-38.8745, -38.8676, -38.9445, -38.9811, 
    -38.9322, -38.9386), lat = c(-14.09437, -14.25664, -14.34518, 
    -14.51857, -14.95994, -15.41684), bmode = c(1.396, 1.375, 1.346, 
    1.248, 1.074, 1.289), bmode.5 = c(1, 1, 1, 1, 1, 1), ID = structure(c(53L, 
    53L, 53L, 53L, 53L, 53L), .Label = c("10946.05", "20162.03", 
    "21792.03", "21800.03", "21810.03", "24640.03", "24641.05", "24642.03", 
    "27258.05", "27259.03", "27261.03", "27261.05", "27261.07", "33000.05", 
    "37231.07", "37234.05", "37288.07", "42521.07", "50682.07", "50687.07", 
    "60004.07", "81122.09", "81123.09", "81124.09", "81125.09", "81126.09", 
    "84497.1", "87632.12", "87759.08", "87761.08", "87762.08", "87763.08", 
    "87764.08", "87765.08", "87766.08", "87768.08", "87768.11", "87769.11", 
    "87770.08", "87773.08", "87773.1", "87773.11", "87774.09", "87774.11", 
    "87775.08", "87776.08", "87776.11", "87777.1", "87778.08", "87783.09", 
    "88724.1", "88727.09", "111868.11", "111871.12", "112702.12", 
    "112712.12", "112714.12", "120947.12", "121189.12", "121192.12", 
    "121196.12", "1211931.12"), class = "factor"), sex = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L), .Label = c("F", "Fc", "M"), class = "factor"), 
        speed = c(0.921691675, 2.990097297, 2.137609076, 3.259383146, 
        8.173674567, 8.413534172), area = structure(c(3L, 3L, 3L, 
        3L, 3L, 3L), .Label = c("AA", "AM", "AR"), class = "factor"), 
        angulo = c(41.027, -43.41, 29.056, 18.241, -7.125, -4.702
        ), angle_rel = c(0.7160619, -0.7576522, 0.5071251, 0.3183724, 
        -0.1243479, -0.08206201), bat.depth = c(-45L, -397L, -32L, 
        -29L, -21L, -3L), dist = c(5.53015005, 17.94058378, 12.82565446, 
        19.55629887, 49.0420474, 50.48120503), angulo_modulo = c(41.027, 
        43.41, 29.056, 18.241, 7.125, 4.702), curva = structure(c(1L, 
        1L, 1L, 1L, 1L, 1L), .Label = c("curva", "reta"), class = "factor")), row.names = 2:7, class = "data.frame")