0

这是我在这里的第一篇文章,希望我能说清楚!我正在尝试对我的 ggplot 进行非常微妙的更改。我正在绘制以 (x,y) 为中心的十字准线,其中 SD 显示为误差线,但我的复杂之处在于比例和线型。我有一个数据框(z),其中每一行都是多行的聚合。每行将在 ggplot 中绘制为具有自己颜色的十字准线,但是,数据框的某些行应绘制为法线,而其他行应绘制为虚线。两列 groupnum 和 groupttl 分别具有应该在比例中显示的颜色和标签的数值,除了颜色之外,我还想在比例栏中显示线型。但我还无法弄清楚这一点!

这是我的代码正在做的事情:

chp <- ggplot(data=z,aes(x=x,y=y,colour = group,group = group,linetype=factor(othr))) + 
    theme_bw() + #remove background
    xlab(m1) +
    ylab(m2) +
    geom_errorbar(aes(ymin = y - ySD, ymax = y + ySD, linetype = factor(othr))) + 
    geom_errorbarh(aes(xmin = x - xSD, xmax = x + xSD, linetype = factor(othr))) +
    guides(linetype=FALSE) + #remove the legend added by linetype
    scale_colour_manual(name=hierarchyname, values=factor(unique(groupnum))) + #add colors manually
    scale_linetype_manual(values = factor(unique(othr)))

注意: othr 只有两个值 (1,2) 我只想显示两种类型的线——普通线和虚线。

我找到的最接近的匹配是:Passing variable with line types to ggplot linetype

他们说的诀窍是结合使用 line_type = geography 和 scale_linetype_manual(values = lty)。但就我而言,不同之处在于我不希望每条线都有自己的线型,我只需要两条。

我真的很感激对此的任何建议。如果您需要更多信息,请告诉我。谢谢 SP


行。感谢您的建议。这是我的数据框

"groupnum","group","N","x","y","xSE","ySE","xSD","ySD","othr"
"1",1,"a1",874,9.31080482242276,3.78151559519843,0.000919599013717256,0.0010731700399164,0.803729537988882,0.93795061488693,1
"2",2,"a2",323,9.32858454311781,3.85117814673799,0.00434633223522011,0.00262887468315802,1.4038653119761,0.84912652266004,1
"3",3,"a3",401,8.65698443874862,3.61961141000158,0.00320566652230181,0.0024382624307443,1.28547227544303,0.977743234728466,1
"4",4,"a4",364,8.02722008290545,4.03272290234161,0.00664827121575061,0.00285893561941909,2.41997072253322,1.04065256546855,1
"5",5,"a5",2117,9.38192555351799,3.39287371184691,0.000317738758329197,0.000433382027761261,0.672652951382911,0.917469752770589,1
"6",6,"a6",347,9.2600568761331,3.34027019277033,0.00105036291416114,0.0024058297735207,0.364475931213916,0.834822931411683,1
"7",7,"a7",782,9.26685442279479,3.32913948807558,0.000672770018741615,0.00103475341970955,0.526106154655943,0.809177174212869,1
"8",9,"a8",335,9.33774697202318,3.83086473063289,0.0022406672509349,0.0033135194052142,0.750623529063193,1.11002900074676,1
"9",10,"a9",588,9.2362799520667,3.79905936514298,0.000648139660555769,0.00112763352751969,0.381106120406792,0.663048514181577,1
"10",8,"b1",631,9.39770259697672,3.49584647787853,0.00147287975375933,0.00176346535893738,0.929387124622137,1.11274664148949,2

可以使用命令读取

read.table(file = "zp.txt",sep = ",", header = TRUE, stringsAsFactors = FALSE)

另一个要求是:我想使用

scale_colour_hue(name=hierarchyname, # Legend label, use darker colors
             breaks=unique(group),
             labels=unique(group),#add the legend for all groups
             l=50,       # Use darker colors, lightness=50
             c=100)        #chroma (intensity of color)

而不是 scale_colour_manual 由于更好的颜色,但是,这是一个次要问题,首先我希望修复比例!

好的也可以编辑它!谢谢 SP

4

1 回答 1

4

没有你的数据框z来检查它是不可能知道的,但我认为如果你改变这将起作用:

 scale_linetype_manual(values = factor(unique(othr))

 scale_linetype_manual(values =c(1,2))

编辑

以下似乎可以满足您的需求:

chp <- ggplot(data=z,aes(x=x,y=y,colour = group, linetype=as.factor(othr))) + 
  geom_errorbar(aes(ymin = y - ySD, ymax = y + ySD)) + 
  geom_errorbarh(aes(xmin = x - xSD, xmax = x + xSD)) +
  theme_bw() +
  guides(linetype=FALSE)
chp

在此处输入图像描述


没有guides(linetype=FALSE)在此处输入图像描述


手动解决方案

这将允许您指定色标图例的线型。

guides(colour=guide_legend(override.aes=list(linetype=c(2,rep(1,times=9))))) 

override.aes参数guide_legend()允许您调整列表中的图例特征。Solinetype被指定为 2 和 9 个(虚线是linetype=2,实线是linetype = 1)。这就是我所说的手动。

在此处输入图像描述

于 2013-02-25T04:09:50.520 回答