Basically like this question:
How can I set axis ranges in ggplot2 when using a log scale?
The issue I have with the solution and ggplot2 it seems that it overdraws. I want limits = c(1,1e8) I do not want the tiny bit additional space below and above this limits. See below:
I want the limits to be at the edge of the graph and 0 additional space. How do I achieve that?
Try:
+ scale_y_log10(limits = c(1, 1e8), expand = c(0, 0))
although personally, I think the extra spacing serves a purpose.
ggplot(data = df,aes(x = x, y =y)) +
geom_point() +
scale_y_log10(limits = c(1,1e8), expand = c(0, 0))
Related
Novice R user here wrestling with some arcane details of ggplot
I am trying to produce a plot that charts two data ranges: One plotted as a line, and another plotted on the same plot, but as points. The code is something roughly like this:
ggplot(data1, aes(x = Year, y = Capacity, col = Process)) +
geom_line() +
facet_grid(Country ~ ., scales = "free_y") +
scale_y_continuous(trans = "log10") +
geom_point(data = data2, aes(x = Year, y = Capacity, col = Process))
I've left out some additional cosmetic arguments for the sake of simplicity.
The problem is that the points from the geom_point keep getting cut off by the x axis:
I know the standard fix here would be to adjust the y limits to make room for the points:
scale_y_continuous(limits = c(-100, Y_MAX))
But here there is a separate problem due to the facet grid with free scales, since there is no single value for Y_MAX
I've also tried it using expansions:
scale_y_continuous(expand = c(0.5, 0))
But here, it runs into problems with the log scale, since it multiplies by different values for each facet, producing very wonky results.
I just want to produce enough blank space on the bottom of each facet to make room for the point. Or, alternatively, move each point up a little bit to make room. Is there any easy way to do this in my case?
This might be a good place for scales::pseudo_log_trans, which combines a log transformation with a linear transformation (and a flipped sign log transformation) to retain most of the benefits of a log transformation while also allowing zero and negative values. Adjust the sigma parameter of the function to adjust where the transition from linear to log should happen.
library(ggplot2)
ggplot(data = data.frame(country = rep(c("France","USA"), each = 5),
x = rep(1:5, times = 2),
y = c(10^(2:6), 0, 10^(1:4))),
aes(x,y)) +
geom_point() +
# scale_y_continuous(trans = "log10") +
scale_y_continuous(trans = scales::pseudo_log_trans(),
breaks = c(0, 10^(0:6)),
labels = scales::label_number_si()) +
facet_wrap(~country, ncol = 1, scales = "free_y")
vs. with (trans = "log10"):
I am trying to change both the yaxis scale and the amount of decimal places. I am using ylim() (to change y scale) and scale_y_continuous(labels = scales::number_format(accuracy = 0.01)) (from scale package to change the decimal points) but they wont work together. I am using ggplot to plot my data.
If you use limits in scale_y_continuous, it will work.
Also you may want to use label_number instead of number_format, because, number_format is superseded by label_number as per the documentation,
These functions are kept for backward compatibility; you should switch to label_number()/label_comma() for new code.
library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = drat)) +
geom_point() +
scale_y_continuous(
labels = scales::label_number(accuracy = 0.1),
limits = c(3, 4)
)
(Using mtcars built-in data for demo)
How can I plot this legend with ggplot2?(please figure 2)
Please look at the legend of the interrupt gap between, but they are not equal, I want to let them become equal gap, because my data most distribution between 0 to 3, do that I want to let the color of the gradient between 0 to 3 to see more, that is to say, legend labels is(0,1,2,4,7,10), and that it is on the legend of equal distribution, as shown in right figure 2 legend
My code(but not plot I want legend):
y = c(seq(0.1,3,0.1),4:10)
x = seq(0.1, 5, length.out = length(y))
df = data.frame(x = x, y=y)
ggplot(df, aes(x,y, color = y)) + geom_point() +
scale_color_gradientn(colours = c("blue", "yellow","red"),
values = c(0,scales::rescale(c(0,1,2,5,7,10),
from = range(df$y)),1),
breaks = c(0,1,2,4,7,10), limits = c(0,10))
Created on 2021-12-29 by the reprex package (v2.0.1)
thank you!
For make Pathway Analysis Im use this code, and I put a scale color gradrient.
library(ggplot2)
ggplot(aes(x=`Enrichment Score (-log10(Pvalue))`,y=Term,
size=`Count`, color=`P value`)) +
geom_point(alpha=0.9,stroke = 1)+
scale_color_gradient2(high = '#4101fa',
mid = '#d8006f',
low = '#f20530',
breaks = c(0.002478,0.0012391 ,0.0001511))+
guides(colour = guide_colourbar(reverse = F))
It's not entirely clear to me what you are really intending to get. One reason for this is that you have not provided a minimal, reproducible example. https://stackoverflow.com/help/minimal-reproducible-example
Anyways, I'm trying to guess what you looking for. You need to set breaks in the call to scale_continuous. Also don't forget to set the limits.
library(ggplot2)
ggplot(mtcars, aes(mpg, disp, color = cyl))+
geom_point() +
scale_color_continuous(breaks = c(0, 1, 3, 7, 10), limits = c(0,10))
Created on 2021-12-28 by the reprex package (v2.0.1)
After 1.5-years' searching, I finally find the answer:
using ggplot2 packege: Set both arguments "trans="pseudo_log" " and "guide = guide_coloursteps(even.steps = T)" in scale_color_gradientn, you can get the legend you want.
or 2. using plot: plot first without legend, then use fields::image.plot(zlim=c(0,10),col=rainbow(length(breaks-1)),lab.break=breaks)
Here is my ggplot and I need to adjust the spacing between x axis labels and make the plot larger. Please advise what is missing here? Trying to play with expand but without any luck:
ggplot(device_contacts_jaccard_android_ios, aes(x = factor(uuid_lev), y = j, fill = factor(android))) +
geom_boxplot() +
facet_wrap( ~ dt) +
scale_x_discrete(expand=c(0, 0.5)) +
theme(axis.text.x = element_text(angle = -90, hjust = 0))
I have checked other answers here, no one solved my issue, correct me if I am wrong.
Use ggsave or pdf(...);...; dev.off() to increase the output size of the image. Once you do that, the space between axis text labels should increase. You could also decrease the font using element_text(size = 7), for example.
Note that in ggsave(..., size, width) are in inches, by default. Good starting points are width = 8 and height = 8. If you want to change the units, try ggsave(..., units = "cm").
Making a plot with ggplot, I wish to set my axis exactly. I am aware that I can set the plot range (e.g. for the x-axis I specified limits from 2 to 4) with coord_cartesian() but that leaves a bit of space to the left and right of the range I specify:
Code for the MWE above:
library(ggplot2)
data.mwe = cbind.data.frame(x = 1:5, y = 2:6)
plot.mwe = ggplot(data = data.mwe, aes(x=x,y=y)) + geom_line() + coord_cartesian(xlim = c(2,4))
print(plot.mwe)
My desired result is a plot where the displayed area is exactly between the limits I specify.
I am aware of
How to set limits for axes in ggplot2 R plots?
but it does not answer my question, as it produces the undesired result above, or cuts out observations (with the limits argument to scale_x_continuous). I know I could tinker with setting a smaller range for limits, but I am looking for a clean result. At the least I would like to know by how much the actual range differs from the one I specify, so that I could adapt my limits accordingly.
Add expand = FALSE:
library(ggplot2)
data.mwe = data.frame(x = 1:5,
y = 2:6)
ggplot(data.mwe, aes(x, y)) +
geom_line() +
coord_cartesian(xlim = c(2, 4),
expand = FALSE)