0

我想用ggplot2可视化igraph对象的度数分布。因为ggplot2不采用由I 生成的简单数字向量将其转换为频率表。然后我将它传递给. 我仍然得到:我无法将表格列设置为因子,因为我还需要在对数刻度上绘制它。degree()ggplot()geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?degree

library(igraph)
library(ggplot2)

g <- ba.game(20) 

degree <- degree(g, V(g), mode="in")
degree
# [1] 6 2 7 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0

degree <- data.frame(table(degree))
degree
#   degree Freq
# 1      0   13
# 2      1    4
# 3      2    1
# 4      6    1
# 5      7    1

ggplot(degree, aes(x=degree, y=Freq)) +
  geom_line()
# geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
4

1 回答 1

1

The problem is that you have turned degree$degree into a factor by using table. Two things to fix this:

  • make it a factor with all possible values (up to the largest degree) so that you don't miss the zeros.
  • convert the labels back to numbers before plotting

Implementing those (I used degree.df instead of overwriting degree to keep the different steps distinct):

degree.df <- data.frame(table(degree=factor(degree, levels=seq_len(max(degree)))))
degree.df$degree <- as.numeric(as.character(degree.df$degree))

Then the plotting code is what you had:

ggplot(degree.df, aes(x=degree, y=Freq)) +
  geom_line()

enter image description here

于 2013-09-26T21:08:10.737 回答