透過R語言繪製圖表(3)–圖表的更多設定

Share This Post

排序

  • reorder()
p <- ggplot(data = my.df, aes(x = reorder(type, value), y = value)) +
  geom_bar(stat = "identity")
  • 使用scale_x_discrete()達到反向排序的效果
p + scale_x_discrete(limits = rev)

調整寬度

geom_bar()中的width參數

p <- ggplot(data = my.df, aes(x = type, y = value)) +
  geom_bar(stat = "identity", width = 0.5)

調整顏色

geom_bar()中的fill參數

p <- ggplot(data = my.df, aes(x = type, y = value)) +
  geom_bar(stat = "identity", fill = "#6ab04c")

標示文字

geom_text()

p <- ggplot(data = my.df, aes(x = type, y = value)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = value), vjust = -0.3, size = 5.5) +
  ylim(c(0, 8))

訂閱研究文章

Get updates and learn from the best