0

how I can add geom_text from other dataframe in R

I have two dataframe. And I make geom_bar like this.

ggplot(
  data = sum_slaughter_2021,
  aes(x=month)
) +
  geom_bar(alpha=0.5, aes(y=total_slaughter, fill=kind),position='stack', stat="identity") +
  theme_minimal(base_family = "AppleSDGothicNeo-SemiBold") +
  scale_x_continuous(name = "기간", breaks = seq(1, 12, 1), labels = paste0(seq(1, 12, 1), '월')) +
  theme(
    plot.title = element_text(hjust = 0.5,size=18, color = "royalblue4", face="bold"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_line(size = 0.1, color = "grey"),
    panel.grid.minor.y = element_line(size = 0.1, color = "grey")
  )

enter image description here

This data frame (sum_slaughter_2021) is like this.

enter image description here

I hope add a geom_text from other dataframe on each geom_bar.

Other dataframe is like this.

enter image description here

I try

ggplot(
  data = sum_slaughter_2021,
  aes(x=month)
) +
  geom_bar(alpha=0.5, aes(y=total_slaughter, fill=kind),position='stack', stat="identity") +
  theme_minimal(base_family = "AppleSDGothicNeo-SemiBold") +
  scale_x_continuous(name = "기간", breaks = seq(1, 12, 1), labels = paste0(seq(1, 12, 1), '월')) +
  geom_text(
    data = month_rate_2021,
    aes(label=paste0(round(month_rate,1),"%"), y=4000)
  ) +
  theme(
    plot.title = element_text(hjust = 0.5,size=18, color = "royalblue4", face="bold"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_line(size = 0.1, color = "grey"),
    panel.grid.minor.y = element_line(size = 0.1, color = "grey")
  )

But I want geom_text on geom_bar, not in geom_bar.

enter image description here

So I try other way like this.

ggplot(
  data = sum_slaughter_2021,
  aes(x=month)
) +
  geom_bar(alpha=0.5, aes(y=total_slaughter, fill=kind),position='stack', stat="identity") +
  theme_minimal(base_family = "AppleSDGothicNeo-SemiBold") +
  scale_x_continuous(name = "기간", breaks = seq(1, 12, 1), labels = paste0(seq(1, 12, 1), '월')) +
  geom_text(
    data = month_rate_2021,
    aes(label=paste0(round(month_rate,1),"%"), y=sum_slaughter_2021$total_slaughter)
  ) +
  theme(
    plot.title = element_text(hjust = 0.5,size=18, color = "royalblue4", face="bold"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_line(size = 0.1, color = "grey"),
    panel.grid.minor.y = element_line(size = 0.1, color = "grey")
  )

But I got error like this

Error in `geom_text()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (12)
✖ Fix the following mappings: `y`
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
  • 1
    Welcome to SO, seungwoolee! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data so that others can run your code and figure out a solution. Please do not post an image of data [for these reasons](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). Just include the data directly using e.g. `dput()`. – stefan Feb 14 '23 at 09:39
  • 1
    The issue is that your `month_rate_2021` dataset lacks a column with the `total_slaughter` per month which is needed to place your labels on top of the bars. I can only guess that you compute the month_rate by summarizing `sum_slaughter_2021`. But if my guess is right then one option would be to add a `total_slaughter` column to your `month_rate_2021 ` data when summarizing. – stefan Feb 14 '23 at 09:42
  • 1
    @stefan Thank you! I didn't know about a minimal reproducible example. And thanks to your advice, I solve this problem. – seungwoolee Feb 15 '23 at 06:18

0 Answers0