I would like to create a scatter plot were each point is graphed as a number instead of a circle. I need a legend that maps the number label in the scatter plot to the sample name.
My current attempt uses this code:
plot<-ggplot(df_mds,aes(x=x,y=y,group=interaction(Group,Sample)))+geom_text(aes(label=Label,color=Group,group=Sample),show.legend =T)
plot<-plot+guide()
plot<-plot+labs(x="Leading logFC dimension 1", y="Leading logFC dimension 2")
Using this data:
And creates this graph:
The graph above is almost exactly what I want except I need a legend mapping the values in the "Label" column to the values in the "Sample" column.
I have seen this post but I don't know how to combine the different shapes to create new numbers.
Any help would be greatly appreciated.
What you're looking for is the ability to create a legend based on the label=
aesthetic. Unfortunately, the answer is that you cannot - at least not easily using ggplot
. I would suggest using alternative approaches. Here I present two options using the following example dataset:
scatter.plot <- data.frame(
sampleName=paste0('Sample_',1:18),
x=sample(1:10, 18, replace=TRUE),
y=sample(1:10, 18, replace=TRUE),
id=1:18,
grp=c(rep('Group A', 6), rep('Group B', 6), rep('Group C',6))
)
ggrepel
package to avoid overlapping labels. Your sample names are short enough where it makes sense that you can add your labels on the plot itself. It can get a bit crowded, so I would recommend using the geom_text_repel
and geom_label_repel
functions in the ggrepel
package to help with overplotting. Aesthetics and various parameters can be adjusted to your liking:
ggplot(scatter.plot, aes(x,y)) +
geom_point(aes(color=grp), size=2.5) +
geom_text_repel(
aes(label=sampleName), color='gray45',
min.segment.length = 0, size=3,
force=10, direction='y') +
theme_bw()
Not sure if this would work for you, but if you adjust the shape for your group and shape for the sample, you can get a legend for each sample out of the box using ggplot
fairly easily. Of course, it needs color to be able to discriminate properly:
ggplot(scatter.plot, aes(x,y)) +
geom_point(aes(shape=grp, color=sampleName), size=2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.