【问题标题】:Add sequential arrows to a ggplot bubbleplot将顺序箭头添加到 ggplot 气泡图
【发布时间】:2013-12-27 01:49:26
【问题描述】:

我有一个线图,上面放了一层气泡。在我覆盖气泡之前,我可以使用this advice 将每个点用箭头连接起来以显示顺序关系

但是现在我已经覆盖了我的气泡,我仍然想像以前一样用箭头连接每个气泡。

这是我的数据:

X <- c(-0.373,-0.256,-0.272,0.048,0.219,0.313,0.209,0.112)
Y <- c(-0.055,-0.091,0.100,0.153,-0.139,-0.004,0.040,-0.004)
Size <- c(37,31,25,10,5,4,6,10)
Label <- c(1,2,3,4,5,6,7,8)

DF <- data.frame(X,Y,Size,Label)

使用上面的建议,我可以尝试绘制一个用箭头连接每个气泡的图,但是气泡的大小会掩盖箭头。

ggplot(DF,aes(x=X, y=Y, size=Size,label=Label),legend=FALSE) + 
  geom_segment(aes(xend=c(tail(X,n=-1),NA), yend=c(tail(Y,n=-1),NA)), 
                   size=0.3, arrow=arrow(length=unit(0.3,'cm'))) + 
  geom_point(color='darkblue',fill="red", shape=21) +
  geom_text(size=2) +
  theme_bw() +
  scale_size(range = c(4, 30), name="Size", breaks=c(10,25,50),
             limits = c(1, 100))

我基本上喜欢上面的情节,但箭头是可见的。我知道可以将箭头写在气泡上方,这样我就可以看到每个箭头,但这不是我想要的。我想要的是从一个气泡的外边缘到下一个气泡的外边缘绘制的箭头。所以我需要通过它指向的气泡半径来缩短每个箭头的头部。

我不知道为什么最后会收到警告

Removed 1 rows containing missing values (geom_segment).

【问题讨论】:

  • Removed 1 rows containing missing values (geom_segment). 这通常意味着您的数据超出了您设置的某些限制。
  • 看到您在geom_segement 调用中添加 NAs 到数据中,这些被删除并不奇怪,这就是警告的含义。

标签: r ggplot2 bubble-chart


【解决方案1】:

你可以从以下开始:

Size_penalty <- 1000
X <- c(-0.373,-0.256,-0.272,0.048,0.219,0.313,0.209,0.112)
X_next <- c(X[-1], NA)
Y <- c(-0.055,-0.091,0.100,0.153,-0.139,-0.004,0.040,-0.004)
Y_next <- c(Y[-1], NA)
Arrow_length <- sqrt((X - X_next)^2 + (Y - Y_next)^2)
Size <- c(37,31,25,10,5,4,6,10)
Size_next <- c(Size[-1], NA)
X_begin <- X + Size / Size_penalty * (X_next - X) / Arrow_length 
Y_begin <- Y + Size / Size_penalty * (Y_next - Y) / Arrow_length 
X_end <- X_next + Size_next / Size_penalty * (X - X_next) / Arrow_length 
Y_end <- Y_next + Size_next / Size_penalty * (Y - Y_next) / Arrow_length 
Label <- c(1,2,3,4,5,6,7,8)
DF <- data.frame(X, Y, X_begin, Y_begin, X_end, Y_end, Size, Label)

ggplot(DF, aes(x=X, y=Y, size=Size, label=Label),legend=FALSE) + 
  geom_point(color='darkblue', fill="red", shape=21) +
  geom_segment(aes(x=X_begin, y=Y_begin, xend=X_end, yend=Y_end), 
               size=0.3, arrow=arrow(length=unit(0.3, 'cm'))) + 
  geom_text(size=4) +
  theme_bw() + 
  scale_size(range = c(4, 30), name="Size", breaks=c(10, 25, 50),
             limits = c(1, 60))

这里我使用 Size / Size_penalty 作为气泡半径的代理,这显然远非优雅。但这是我能做的最好的,因为有一个 scale_size,所以从大小到半径的转换是隐式的。剩下的就是找到一个像

这样的转换函数
rad <- function(ggplot_size_after_scaling) {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2019-02-24
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多