delete empty right margin of a plot (ggplot2)

Hi, ?'m a biology student and right now I'm working on my master degree thesis.
I'm using ggplot2 to plot a large table of values like this:

V1 V2 V3 V3 V5
1_start=101;end=200;length=100;source_length=4103074 2 0 0.00 0.00
1_start=201;end=300;length=100;source_length=4103074 3 14 92.90 93.85
1_start=301;end=400;length=100;source_length=4103074 4 19 87.33 93.48
1_start=401;end=500;length=100;source_length=4103074 5 6 90.46 94.87
1_start=501;end=600;length=100;source_length=4103074 6 7 90.67 100.00
1_start=601;end=700;length=100;source_length=4103074 7 12 89.83 97.06
1_start=701;end=800;length=100;source_length=4103074 8 0 0.00 0.00

The values are 57515, but the plot shows an empty right margin to 60000, so I need to delete this empty space. How i can do it?

the plot is this:

also, I need to multiply the values ​​on the x axis by 100. (for example the 40000 label in the plot must be 4000000). How I can do it?

here it is the reprex

ggplot (table1, aes(x=V2, y=V3)) + geom_bar(stat = "identity", width = 0.01, color = "purple2") + labs(title = "HITS NUMBER ALONG REFERENCE GENOME",
                                                                                                   subtitle = "Height by number of hits for the same position ",
                                                                                                   caption = "Data source: ignote bacillus 01 vs B.Simplex NBRC 15720")+
    theme(
        plot.title = element_text(hjust = 0.5, size = 14),    # Center title position and size
        plot.subtitle = element_text(hjust = 0.5),            # Center subtitle
        plot.caption = element_text(hjust = 0, face = "italic")# move caption to the left
    ) +  ylab("repeated hits") + xlab("position")
#> Error in ggplot(table1, aes(x = V2, y = V3)): non trovo la funzione "ggplot"

Created on 2019-11-30 by the reprex package (v0.3.0)

Thank you all

Hi,
Could you share the sample data on a copy/paste friendly format?, the data you have posted doesn't seem to correspond to the plot you are showing (there are two columns named V3 and they are on a much smaller scale).

Try to turn your example into a self-contained reproducible example, as explained in this guide

Hi thank you for the reply, there is my data. I'm using read.table (filename, header=false) to import my data table on R, so it auto assigns coloums names on it (V1, V2, V3, V4, V5). first column is just an ID given by the alignment tool (it's useless so i deleted it on the pastebin link).
On the data attached on pastebin, first column is the position along my genome, second column is the number of hits obtained by mine alignment tool on that position, third is the average value and fourth column is the maximun value. I have to do 3 plots, but this isn't a problem since I only need to switch the Y.

https://pastebin.com/thrHbrc5

I've got in total 57515 positions to plot, but the graph generated by ggplot2 extends it to 60000, leaving an empty space on it. I need to delete this empty space and make ggplot2 stop exactly at the 57515 position. I can't put this plot on my thesis with that empty space (shown in the pic attached previusly).

The following will get rid of empty space on the left and the right.

 + scale_x_continuous(expand=c(0,0))

If you want specific amounts of padding at each end, you can control that with, for example:

 + scale_x_continuous(expand=expand_scale(mult=c(0.01, 0.01)))

which will add padding on each side equal to 1% of the data range.

To modify the x-axis labels, you can multiply V2 by 1,000 in the data frame itself, but you can also use the labels argument in scale_x_continuous to modify the labels. For example:

+ scale_x_continuous(expand=c(0,0), labels=function(x) 1000*x))

will remove the padding at each end and multiply the x-axis label values by 1,000.

2 Likes

Thank You very much!! it worked! :grin::grin::grin:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.