I want to apply geom_text to a particular set of variables.
I have, for example:
decade count
<dbl> <int>
1 1930 505
2 1940 630
3 1950 806
4 1960 446
5 1970 469
6 1980 807
7 1990 1057
8 2000 1856
9 2010 2133
My plot looks like this: My plot
So, I want to add some labels to each bar, where the year has to be shown. For the bars with a value of >= 500, I want the label to be inside the bar, for the rest I want it to be outside.
I tried to do this with geom_text:
geom_col(fill = THISBLUE,
width = 0.7) +
geom_text(data = subset(data, count >= 500)
aes(0, y = name, label = name))
However, I get this error message:
Error in count >= 500 : comparison (5) is possible only for atomic and list types
How about this approach?
ggplot(df,aes(decade,count)) +
geom_col(fill = "blue", width = 4) +
coord_flip() +
geom_text(data = subset(df, count >= 500), aes(label = count),nudge_y = -100,color="white") +
geom_text(data = subset(df, count < 500), aes(label = count),nudge_y = 100,color="black")
Input:
df =tribble(
~decade,~count,
1930, 505,
1940, 630,
1950, 806,
1960, 446,
1970, 469,
1980, 807,
1990, 1057,
2000, 1856,
2010, 2133
)