【问题标题】:Color one point with several colors in r ggplot2在r ggplot2中用几种颜色着色一个点
【发布时间】:2019-12-16 17:38:19
【问题描述】:

如何绘制附图,即将 geom_point 绘制为雷达/星?不一定为每个段显示不同的大小。

【问题讨论】:

  • 仅在 ggplot2 中并不简单。 ggforce 附加包中的这个 geom 看起来很有帮助:ggforce.data-imaginist.com/reference/geom_arc_bar.html
  • 您能否添加一些您想要绘制的数据。看起来很有趣的任务
  • 感谢您的回复。假设我们用红色表示 A,蓝色表示 B,绿色表示 C。只是为了绘制点“AB”(1/2red 和 1/2 blue),“AC”(1/2red,1/2 green)和“ABC” (1/3 红色、1/3 蓝色和 1/3 绿色)

标签: r ggplot2


【解决方案1】:

感谢您的数据!

按照 Jon Spring 的建议,我与 ggforce 玩了一会儿(上面的评论)

library(ggforce)

df=data.frame(point=c("AB","AC","ABC"),
              x=1:3, # x coordinate of point center
              y=c(0.5,2,0.5),# y coordinate of point center
              A=c(0.5,0.5,1/3), # I chose different values 
              B=c(0.7,0,1/4),   # to make the resulting plot
              C=c(0,0.5,1/3))   # a little more interesting

df=pivot_longer(df,cols=c(A,B,C),names_to = "colorby",values_to = "amount")
df$radius=sqrt(df$amount) # since we have a number represented by area
ggplot(df)+
  geom_polygon(aes(x=x,y=y),color="black",fill=NA,size=2)+ # draws the line connecting the points
  geom_arc_bar(aes(x0=x,y0=y,r0=0,r=radius,amount=amount,fill=colorby),stat="pie")+
  coord_equal()+ # so one gets actually circles
  scale_fill_manual(values = c("A"="red","B"="blue","C"="green"))

如果你只想要“饼图”,你可以设置r=1(或其他合适的值)

【讨论】:

    【解决方案2】:

    我认为这与您在 ggplot2 中所能获得的一样接近:

    sizes <- expand.grid(size = 8, stroke = 2)
    ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) + 
    geom_point(shape = 16, colour = "red") +
    geom_point(shape = 10, colour = "blue")
    

    sizes <- expand.grid(size = 8, stroke = 2)
    ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) + 
    geom_point(shape = 21, fill = "red", colour = "blue") +
    geom_point(shape = 10, colour = "blue")
    

    您可以获得具有多种颜色的雷达/星星,但不能超过两种颜色(边框和填充)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多