#图1 基本图 library(ggplot2) # Keep 30 first rows in the mtcars natively available dataset data=head(mtcars, 30) # 1/ add text with geom_text, use nudge to nudge the text ggplot(data, aes(x=wt, y=mpg)) + geom_point() + # Show dots geom_text( label=rownames(data), nudge_x = 0.25, nudge_y = 0.25, check_overlap = T )
# 图2 library(ggplot2) # Keep 30 first rows in the mtcars natively available dataset data=head(mtcars, 30) # 1/ add text with geom_text, use nudge to nudge the text ggplot(data, aes(x=wt, y=mpg)) + geom_point() + # Show dots geom_label( label=rownames(data), nudge_x = 0.25, nudge_y = 0.25, check_overlap = T )
图2
仅添加一个文本标签时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#图3 library(ggplot2) # Keep 30 first rows in the mtcars natively available dataset data=head(mtcars, 30) # Add one annotation ggplot(data, aes(x=wt, y=mpg)) + geom_point() + # Show dots geom_label( label="Look at this!", x=4.1, y=20, label.padding = unit(0.55, "lines"), # Rectangle size around label label.size = 0.35, color = "black", fill="#69b3a2" )
# Keep 30 first rows in the mtcars natively available dataset data=head(mtcars, 30)
# Change data rownames as a real column called 'carName' data <- data %>% rownames_to_column(var="carName") # Plot ggplot(data, aes(x=wt, y=mpg)) + geom_point() + geom_label( data=data %>% filter(mpg>20 & wt>3), # Filter data first aes(label=carName) )