I have the following dataframe:
foo = read.table(text="
protein1 1.51E-02 5 label1
protein2 9.06E-05 850 label2
protein3 3.38E-05 1155 label1
protein4 3.38E-05 1154 label1
protein5 6.64E-03 35 label3
protein6 5.32E-03 44 label4",
sep="\t", col.names=c("category", "value", "rank", "ToLabel"))
And I want to make a bar plot of rank
(x axis) vs. value
(y axis), and add a label that is stored in the column ToLabel
.
This is my code for plotting:
ggplot(data=foo, aes(x=rank, y=value))
geom_bar(stat="identity")
geom_col(fill="dark grey")
labs(x = "Rank",
y = "Value")
theme_bw()
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
scale_x_continuous(expand = c(0, 0)) scale_y_continuous(expand = c(0, 0))
scale_fill_manual( values = c( "yes"="red", "no"="black" ), guide = "none" )
geom_pointrange(data = foo, aes(x=rank, y=value, ymin = 0, ymax = 0))
#geom_text(data = genes_df, aes(x=rank, y=relative_iBAQ, label = ToText), vjust = 0, angle=45, size = 1)
geom_text(data = foo, aes(label = ToLabel),
position = position_stack(vjust = 1.1), angle=45, size = 3)
Unfortunately, the labels are not positioned inside my plotting area:
I have so far not found a way to move them. Any idea how to position the labels better?
CodePudding user response:
I added xlim and ylim values. But you don't need to do this if you remove expand(0,0) Thomas noted.
xlim(-100, 1100)
ylim(-.001, .02)
CodePudding user response:
You can use the nudge_x
and nudge_y
arguments to adjust the text, but you have to stop using position
for that to work. See the ?geom_text
help page for details.
I'd also recommend not using expand(0, 0)
for the x-axis so there's a bit of padding where your labels can go.
And you can control the justification of the text with hjust
. The default hjust = 0.5
centers the text on its coordinate. Setting hjust = 0
left justifies the text at its coordinate.