这是我在这里发表的第一篇文章,希望我能说清楚!我正试图对我的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
答案 0 :(得分: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
的{{1}}参数允许您调整列表中的图例特征。因此guide_legend()
指定为2和9(虚线为linetype
,实数为linetype=2
)。这就是我的意思。