我的数据框df
看起来像这样
Year Frequency
1 1975 86
2 1976 52
3 1977 53
4 1978 54
5 1979 301
6 1980 161
您可以使用它来自己复制data.frame:
ydf <- structure(list(Year = c(1975, 1976, 1977, 1978, 1979, 1980),
Frequency = c(86, 52, 53, 54, 301, 161)),
row.names = c(NA, -6L), class = "data.frame")
我用这个图
ggplot(ydf, aes(x = Year, y = Frequency, fill=Frequency)) + geom_bar(stat = "identity") + geom_text(aes(label = Frequency), nudge_y=1, check_overlap=TRUE)+ scale_x_discrete(guide = guide_axis(angle = 90))
但是,您可以看到条形上方的频率重叠。我试图将x轴分开或像xlabel一样旋转Frequency 90。我做了很多谷歌搜索,但是没有运气。我是R的新手,所以我的绘图代码可能不正确或可以做得更好。请注意,我喜欢将图例显示为一个条,并且不想更改它。
使用@ markus,nudge_y和angle为标签建议的geom_col:
library(ggplot2)
set.seed(42)
# Making up data
my_df <- tibble::tibble(year = 1:25, freq = sample(50:400, replace = T, size = 25))
# a variable to change nudge_y based on data's range
range = max(my_df$freq) -min(my_df$freq)
#Plotting
ggplot(my_df, aes(x = year, y = freq)) +
geom_col(aes(fill = freq)) +
geom_text(aes(label = freq),
angle = 90,
nudge_y = range / 20) # the 20 may need adjusting for data with significantly different range
由reprex软件包(v0.3.0)创建于2020-12-27
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句