我正在嘗試繪制一個圖表,其中 df 列中的 TRUE 陳述句數。
我有一個看起來像這樣的df
Speed Month_1
12 67
12 114
12 155
12 44
13 77
13 165
13 114
13 177
...
我想繪制一個條形圖,其中我們有 x = Speed 和 y = Month_1 列中超過 100 的行數。
因此,對于 X = 12,我將有一個 Y 值為 2 的條形圖,對于 X = 13,我將有一個 Y 值為 3。
我可以直接在 ggplot 中執行此操作,還是必須先創建一個新的 DF?
uj5u.com熱心網友回復:
當然,只需過濾掉您傳遞給的資料中低于 100 的值ggplot并執行正常操作geom_bar
ggplot(df[df$Month_1 >= 100, ], aes(factor(Speed)))
geom_bar(width = 0.5, fill = 'deepskyblue4')
theme_bw(base_size = 16)
labs(x = 'Speed')

如果由于某種原因,您確實需要傳遞完整的資料幀而不過濾它,您可以用完全透明的顏色填充 < 100 的值:
ggplot(df, aes(factor(Speed), fill = Month_1 > 100))
geom_bar(width = 0.5)
theme_bw(base_size = 16)
scale_fill_manual(values = c('#00000000', 'deepskyblue4'))
labs(x = 'Speed')
theme(legend.position = 'none')

uj5u.com熱心網友回復:
您可以使用dplyr過濾您的資料框,然后用ggplot.
library(tidyverse)
df <- tibble(Speed = c(12, 12, 12, 12, 13, 13, 13, 13),
Month_1 = c(67, 114, 155, 44, 77, 165, 114, 177))
df %>% filter(Month_1 > 100) %>%
ggplot(aes(x = Speed)) geom_bar()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/478907.html
上一篇:Plotly中的金字塔圖
