如何在ggplot2中绘制一个不错的箭头 [英] How to draw a nice arrow in ggplot2

查看:3094
本文介绍了如何在ggplot2中绘制一个不错的箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 ggplot 图表,我希望在两点之间有一些箭头。主要任务可以通过 geom_line(arrow = arrow())轻松完成。但是,我想要一些漂亮的粗箭头。通过 size = 调整箭头的大小并没有帮助,因为它会彻底弄乱箭头。我举例说明了我的问题:

创建一些示例数据和一个图:

 <$ c $ (2016年,2011年,2016年,2011年,2011年,2011年,2011年, (年)
VALUE <-c(1,4,1,5,2,8)
DATA < - 数据。 frame(NAME,YEAR,VALUE)

ggplot(DATA,aes(x = VALUE,y = NAME))+
geom_point(size = 5,aes(color = YEAR))+
geom_line(arrow = arrow(length = unit(0.30,cm),ends =first,type =closed))

结果图如下所示:



现在我试着加厚箭头......

  ggplot(DATA,aes(x = VALUE,y = NAME))+ 
geom_point(size = 5,aes(color = YEAR))+
geom_line(arrow = arrow(length = unit(0.30,cm),ends =first,type =closed),size = 3)
pre>

结果如下所示:





我的问题:有什么办法可以绘制一些漂亮的粗箭头吗?

我通常使用 geom_segment 创建箭头。但要做到这一点,我们需要修改数据从长到宽格式(通常在中使用 dcast > reshape2 data.table 包)。但是这次我尝试使用 base reshape 函数。

  ggplot(DATA,aes(x = VALUE,y = NAME))+ 
geom_point(size = 5,aes(color = YEAR))+
geom_segment(data = reshape(DATA,v.names =VALUE,idvar =NAME,timevar =YEAR,direction =wide),
aes(x = VALUE.2011,xend = VALUE .2016,y = NAME,yend = NAME),size = 2,
arrow = arrow(length = unit(0.5,cm)))



编辑:I刚发现同样的问题属于封闭型箭头。现在,尝试将图保存为矢量图(pdf或svg,使用 ggsave 或绘图选项卡中的导出菜单)。结果并不杂乱。




I am creating a ggplot chart where I want to have some arrows between two points. The main task is easily done with geom_line(arrow = arrow()). However, I want to have some "beautiful" thick arrows. Resizing the arrow via size= doesn't help since it messes up the head of the arrow completely. I illustrate my Problems:

Create some sample data and a plot:

 NAME <- c("A", "A", "B", "B", "C", "C")
 YEAR <- c(2016, 2011, 2016, 2011, 2016, 2011)
 YEAR <- as.factor(YEAR)
 VALUE <- c(1, 4, 1, 5, 2, 8)
 DATA <- data.frame(NAME, YEAR, VALUE)

ggplot(DATA, aes(x=VALUE, y=NAME)) + 
  geom_point(size=5, aes(colour=YEAR)) +
  geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type = "closed"))

The resulting plot looks like that:

Now I've tried to "thicken" the arrows...

ggplot(DATA, aes(x=VALUE, y=NAME)) + 
  geom_point(size=5, aes(colour=YEAR)) +
  geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type = "closed"), size = 3)

That's the result shown here:

My question: Is there any way to plot some "beautiful" thick arrows?

解决方案

I usually use geom_segment to create arrow. But to do that we need to modify the data from "long" to "wide" format (usually using dcast from reshape2 or data.table package). But this time I tried using base's reshape function.

ggplot(DATA, aes(x=VALUE, y=NAME)) + 
  geom_point(size=5, aes(colour=YEAR)) +
  geom_segment(data = reshape(DATA, v.names="VALUE", idvar = "NAME", timevar = "YEAR", direction = "wide"),
               aes(x=VALUE.2011, xend=VALUE.2016, y=NAME, yend=NAME), size = 2,
               arrow = arrow(length = unit(0.5, "cm")))

EDIT: I just found that same issue pertains for "closed" type arrows. For now, try to save the plot as a vector graph (pdf or svg, using ggsave or Export menu in Plots tab). The result is not "messy".

这篇关于如何在ggplot2中绘制一个不错的箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆