Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

r - Filled and hollow shapes where the fill color = the line color

I want to convey three types of information in a dot-plot. I can use color, shape and fill (my actual data has too many points to use size effectively). But it would look best if the fill color was the same as the outline color.

The closest I can get is this:

data(mtcars)
p <- ggplot(mtcars,aes(x=mpg,y=wt))+
  geom_point(aes(color=factor(cyl),shape=factor(gear),fill=factor(vs)))+
  scale_fill_manual(values=c("black",NA))+scale_shape_manual(values=c(21,22,23))

enter image description here

which fills black into all outline colors, ugly. Any ideas on how to fill the red points with red and the blue points with blue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Introduce NA, and map those to the NA color with scale_fill_discrete:

ggplot(mtcars,aes(x=mpg,y=wt)) +
  geom_point(size=10,
    aes(
      color=factor(cyl), 
      shape=factor(gear), 
      fill=factor(ifelse(vs, NA, cyl))            # <---- NOTE THIS
  ) ) +
  scale_shape_manual(values=c(21,22,23)) +
  scale_fill_discrete(na.value=NA, guide="none")  # <---- NOTE THIS

Produces:

enter image description here


EDIT: To address Mr Flick, we can cheat and add layers / alpha. Note we need to add a layer because as far as I know there is no way to control alpha independently for color and fill:

library(ggplot2)
ggplot(mtcars,aes(x=mpg,y=wt, color=factor(cyl), shape=factor(gear))) +
  geom_point(size=10, aes(fill=factor(cyl), alpha=as.character(vs))) +
  geom_point(size=10) +      
  scale_shape_manual(values=c(21,22,23)) +
  scale_alpha_manual(values=c("1"=0, "0"=1))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...