I created a plot in R with ggplot2, however if I want to change the scale of the y-axis my plot shifts down (see second image). So if I specify the scale of the y-axis, the 0 will be below the plot and the distance between the title and plot will increase. How can I get my plot back up? So, it looks like the first picture? And everything is visible.
Code first plot:
plot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
geom_count() +
labs(title = "Iris dataset",
x = "Sepal Length",
y = "Sepal Width") +
scale_y_discrete(limits = c(0:5))
plot
Without specifying the scale of the y-axis, my plot looks like:
If, I specify the scale of the y-axis with scale_y_discrete(limits = c(0:4))
my plot looks like this: Code
plot 2: plot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
geom_count() +
labs(title = "Iris dataset",
x = "Sepal Length",
y = "Sepal Width") +
scale_y_discrete(limits = c(0:5))
plot
I would instead use ylim()
:
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
geom_count() +
labs(title = "Iris dataset",
x = "Sepal Length",
y = "Sepal Width") +
ylim(0, 5)
From help("scale_y_discrete")
:
For simple manipulation of scale labels and limits, you may wish to use labs() and lims() instead.
You can alternatively use scale_y_continuous()
as mentioned in a comment if you change limits = c(0:5)
to limits = c(0, 5)
:
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
geom_count() +
labs(title = "Iris dataset",
x = "Sepal Length",
y = "Sepal Width") +
scale_y_continuous(limits = c(0, 5))
But, still, my preference would by ylim()
since you're not doing anything else to the scale.
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.