Overflowing X Axis (Ggplot2)

Fix for overflowing x-axis text in ggplot2

Unfortunately you have to manage the y axis yourself - there's currently no way for ggplot2 to figure out how much extra space you need because the physical space required depends on the size of the plot. Use, e.g., expand_limits(y = -0.1) to budget a little extra space for the text.

Overflowing X axis (ggplot2)

I hope this helps. If rotating the axes doesn't work, then you can manually set the breaks and labels of the axis using breaks and labels within scale_x_continuous as follows:

require(ggplot2)
# dummy data
set.seed(45)
len <- 50
df <- data.frame(years = factor(seq(1901, 1950, length.out = len)),
values = 1e6 * runif(len), group=factor(rep(1:5, each=len/5)))
p <- ggplot(data = df, aes(x = years, fill=group)) + geom_bar(aes(weight = values))
require(scales) # for removing scientific notation
p <- p + scale_y_continuous(labels = comma)
# manually generate breaks/labels
labels <- seq(1901, 2000, length.out=10)
# and set breaks and labels
p <- p + scale_x_discrete(breaks=labels, labels=as.character(labels))
p

ggplot2

Force X axis text on for all facets of a facet_grid plot

You can insert a copy of the axes inside the gtable,

library(gtable)
g <- ggplotGrob(p)
# locate the panels
panels <- grep("panel", g$layout$name)
top <- unique(g$layout$t[panels])
# intersperse a copy of the bottom axes
all <- gtable:::rbind_gtable(gtable:::rbind_gtable(g[seq.int(min(top)), ],
g[max(top)+1,], "first"),
g[seq(min(top)+1, nrow(g)),], "first")
grid.newpage()
grid.draw(all)

Sample Image

ggplot2 R, Fixing much values in axis (Line-plot)

You are almost there.. Since your y-axis is a continuous value, you need to use scale_y_continuous instead of scale_y_discrete.

myUglyPlot <- myUglyPlot + scale_y_continuous(breaks = labels)

How to make histogram in ggplot2 start at zero on X axis?

OK, the solution is:

ggplot(data, aes(x = number)) +
geom_histogram(color="black", fill="grey40", binwidth = 0.1,
boundary = 0, closed = "left") +
scale_x_continuous(breaks = c(0, seq(0, 2, 0.1)))

The boundary is the key parameter!

Left and right align some of the tick labels in x-axis of a ggplot

You can specify different hjust() in theme()

Code

library(ggplot2)

ggplot(df, aes(x = levs, y = vals)) +
geom_bar(stat = "identity") +
coord_flip() +
scale_x_discrete(labels = wrap_format(20)) +
scale_y_continuous(breaks = seq(-20000, 20000, by = 10000)) +
theme(axis.text.x = element_text(hjust=c(0, 0.5,0.5,0.5, 1)))

Output

Sample Image

Data

df <- data.frame("levs" = c("a long label i want to wrap",
"another also long label"),
"vals" = c(-20000,20000))

Align labels on data points to x axis

The geom_text() uses also the y = aesthetics, therefore the text is printed at the top of the bars. Suggest you override this by changing the geom_text() as follows:
geom_text(data = pathways_filt, mapping=aes(y = 0, x=reorder(row.names(pathways_filt), -log.qval), label=GO_biological_process_complete), size=3, angle=90, hjust="bottom")



Related Topics



Leave a reply



Submit