0
votes

This is my very first post here, hope I can make myself clear! I am trying to accomplish a very subtle change to my ggplot. I am plotting crosshairs centered on (x,y) with SD showing as errorbars, but the complication i have is with the scale and line types. I have a dataframe (z) where each row is a aggregate over many rows. Each row will be plotted in the ggplot as a crosshair with its own color, however, some rows of the dataframe should be plotted as normal lines, and others should be plotted in dashed lines. Two columns groupnum and groupttl have numeric values for colors and labels respectively that should be shown in the scale, in addition to colors, i also want to show linetype in the scale bar. But I am not able to figure this out yet!

Here is what my code is doing:

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)))

Note: othr has only two values (1,2) i want to show only two types of lines--normal and dashed.

the closest match i found is this: Passing variable with line types to ggplot linetype

where they say The trick is to use use line_type = geography in conjunction with scale_linetype_manual(values = lty). But in my case, the difference is i don't want each line to have its own linetype, I need only two.

I would really appreciate any suggestions on this. Please let me know if you need more info. Thanks SP


Ok. Thanks for suggestions. Here is my dataframe

"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

which can be read using the command

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

Another requirement is: I would like to use

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)

instead of scale_colour_manual due to better colors, however, this is a secondary problem, first i want the scale to be fixed!

ok was able to edit it too! Thanks SP

1
I've edited your post to get your example code to show up nicely: Stack Overflow doesn't use HTML tags for formatting, take a look at these formatting tips for next time: stackoverflow.com/editing-help - Marius
Please make an reproducible example. Currently you have a reasonable word description of the problem but your code is incomplete, and it would be far easier to have a small reproducible data set to use. - mnel
thank you for editing my question. How did you add the line break between the two questions? - user2105887
I've had a look at your data. The remaining problems are: m1 and m2 do not exist, neither does hierarchyname. - alexwhan

1 Answers

4
votes

Without your dataframe z to check it's impossible to know, but I think this will work if you change:

 scale_linetype_manual(values = factor(unique(othr))

to

 scale_linetype_manual(values =c(1,2))

EDIT

The following seems to make what you want:

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

enter image description here


Without guides(linetype=FALSE): enter image description here


Manual Solution

This will let you specify the linetype for the colour scale legend.

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

The override.aes argument of guide_legend() allows you to adjust legend characteristics in a list. So linetype is specified as a 2 and nine ones (dashed is linetype=2, solid is linetype = 1). This is what I mean by manual.

enter image description here