Show the value of a geom_point using geom_text

前端 未结 2 719
醉梦人生
醉梦人生 2020-12-20 08:21

Using this nice code from here:

countries <- structure(list(country = structure(c(5L, 6L, 3L, 4L, 10L, 8L, 
11L, 7L, 1L, 13L, 9L, 12L, 2L), .Label = c(\"A         


        
相关标签:
2条回答
  • 2020-12-20 08:31

    Try this:

    p <- ggplot() + 
      geom_polygon(data=base_world, aes(x=long, y=lat, group=group)) +
      geom_line(data=link1, aes(x=Longitude, y=Latitude), color="blue", size=1) +
      geom_line(data=link2, aes(x=Longitude, y=Latitude), color="green", size=1) +
      geom_point(data=countries, aes(x=Longitude, y=Latitude), colour = "cyan", size=5, alpha=I(0.7)) + #set the color outside of `aes`
      theme(text = element_text(size=20), legend.position="none") +   #remove the legend 
      annotate("text", x = countries$Longitude, y=countries$Latitude, label = countries$Value, hjust = 2, colour = "purple") + 
      annotate("text", x = countries$Longitude, y=countries$Latitude, label = countries$country, hjust = -0.1, colour = "red") #if you also want name of countries
    

    And this would be the plot:

    > p


    P.S. I'd prefer annotate over geom_text as a personal preference but if you want to use that then following works (should reference to the data-frame when defining the coordinates):

    p+geom_text(aes(label=countries$Value,x=countries$Longitude, y=countries$Latitude),hjust=0, vjust=0) 
    

    You may want to consider using different values since texts may not be legible.

    0 讨论(0)
  • 2020-12-20 08:53

    You're missing the data parameter in your geom_text:

    p + geom_text(data = countries, aes(label=Value, x=Longitude, y=Latitude), hjust=0, vjust=0)
    
    0 讨论(0)
提交回复
热议问题