Manually increase values on y axis - r

I want to manually set the range of Y axis. I want manually increase values of Y axis by 30 units. I have a following code:
library(scales)
library(ggplot2)
ggplot(mtcars,aes(x=wt,y=mpg)) +
geom_point() +
geom_smooth() +
scale_y_continuous(trans = ~.+30)
But I got the following error message:
Error in match.fun(f) :
'c("~_trans", ". + 30_trans")' is not a function, character or symbol
Can somebody help me?
This is the plot what I want:

If you just want to change the labels, then this will do it.
ggplot(mtcars, aes(x=wt,y=mpg)) +
geom_point() +
geom_smooth() +
scale_y_continuous(labels=function(x) x+30)

Related

Change y axis values and scale

I am using RStudio.
I´m trying to change the values of the x-axis to 20 - 35 and with an interval of 0,625. However, I don´t seem to find the right code. if I use this code:
ggplot(a1diurnalInflow, aes(x=Time, y=Potential.Air.Temperature....C.,group=1)) +
geom_point() +
geom_line() +
coord_cartesian(ylim = c(0, 35))
The output is weird (see picture two)
Picture one is the plot how it is right now with this code
ggplot(a1diurnalInflow, aes(x=Time, y=Potential.Air.Temperature....C.,group=1)) +
geom_point() +
geom_line()
Kind regards,
Giorgia
♦Current output
Wrong output

How to use geom_text position_stack as part of its aes?

I have the following data frame:
df <- data.frame(x=c(1,2,3,4,5),
y=c(2,3,5,9,9),
label=c('blah1','blah2','blah3','blah4','blah5'),
vjust=c('top','bottom','top','bottom','top'),
posVjust=c(0.9,1.1,0.9,1.1,0.9),
stringsAsFactors=FALSE)
and can plot it directly like so:
p <- ggplot(df, aes(x=x,y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(vjust=vjust))
p
However, I'd like to use the posVjust column as part of geom_text's aes but I can't like so:
geom_text(aes(vjust=vjust,position=position_stack(vjust=posVjust)))
I get the following error:
Warning: Ignoring unknown aesthetics: position
> p
Don't know how to automatically pick scale for object of type
PositionStack/Position/ggproto. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (5): vjust,
position, x, y, label
Is there a way to use my posVjust column as part of the position_stack call?
position isn't an aesthetic and goes outside of aes. As far as I know, position_stack takes a single value, rather than a vector. However, you could change posVjust to be posVjust=c(-0.1,0.1,-0.1,0.1,-0.1) and then do the following:
ggplot(df, aes(x=x, y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(y=y + posVjust))
You could also dispense with posVjust and just do:
ggplot(df, aes(x=x, y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(y=y + c(-0.1,0.1)))
You can add vjust=vjust as well, which will add a small additional increment of vertical offset.
Another option is to remove the points and just use labels instead of point markers. Offsetting the geom_text labels then becomes unnecessary. For example:
ggplot(df, aes(x=x, y=y, label=label)) +
geom_line(linetype="12", colour="grey50") +
geom_text() +
theme_bw()

No line in plot chart despite + geom_line()

I've read documentation and I think that my code should be right, but still there is no line between the points in the output. What is wrong?
The x'axis is discrete and y'axis is continuous.
My code
point.sqrmPrice <- ggplot(overview.df, aes(x = areaSize, y = sqrmPrice)) +
geom_line() +
geom_point() +
scale_y_continuous(breaks = c(seq(min(overview.df$sqrmPrice), max(overview.df$sqrmPrice), by = 10000) )) +
theme_bw()
The underlying issue here is a duplicate of this stack post.
Here's a reproducible example showing what #SN248 meant about adding group to the code
ggplot(iris, aes(x = factor(Sepal.Length), y = Sepal.Width)) +
geom_line(aes(group=1)) + geom_point() + theme_bw()
You are not getting a line because areaSize is a factor. Convert to numeric with
overview.df$areaSize <- as.numeric(as.character(overview.df$areaSize))
and then make the plot.
What you have to think about it is, do you expect a single line to connect all the dots?
Else, how many lines do you expect, that will tell you how many groups will you need to have.
You are missing the group aesthetic required for geom_line(), because you haven't specified how many groups (lines) you want in your plot.

`geom_abline` and `facet_wrap` seem incompatible

I get errors when I use geom_abline in the same plot as facet_wrap or facet_grid, and I don't understand why. For example
# Example data
ex <- data.frame(x=1:10, y=1:10, f=gl(2, 5))
ggplot() +
geom_point(data=ex, aes(x=x, y=y)) +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
causes Error in if (empty(data)) { : missing value where TRUE/FALSE needed.
Above I set the data in the geom_point layer because later on I will add data from a different data frame. This has something to do with the problem, because when I set the data in the base layer I get a different error:
ggplot(ex, aes(x=x, y=y)) +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
Error in as.environment(where) : 'where' is missing
Workaround
There's an easy workaround: If I make a data frame to define a 1:1 line and plot it using geom_line I get essentially the same plot I would have gotten from geom_abline...
# Define a 1:1 line with data
one_to_one <- data.frame(xO=range(ex$totalcells), yO=range(ex$totalcells))
# Plot the 1:1 line with geom_line
ggplot() +
geom_point(data=ex, aes(x=x, y=y)) +
geom_line(data=one_to_one, aes(x=xO, y=yO), colour="black") +
facet_wrap(~f)
...so this question is more about why those errors arise (and whether they represent a bug or expected behavior) rather than how to work around the problem.
The following works:
ggplot(ex, aes(x=x, y=y)) + geom_point() +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
Note the additional geom_point() I added, based on your second example.

ggplot removes all my data when I set x and y limits

I'm using R to generate a plot from a table.
I used the command a <- read.table("table.txt") and that worked fine. When I type "a" it prints out my full table correctly.
I also entered library(ggplot2) so I could use ggplot.
Here was my first command:
ggplot(a, aes(x=V2, y=V5, group=V7)) +
geom_point(size=4, aes(col=V7)) + xlab("Rank") +
ylab("Inter-helix angle (Degree)") +
opts(legend.position="none")
this command generated all the points, but when I added xlim(0,110) + ylim(0,110) to the end, it gave me a warning and said "Removed 101 Rows containing missing values" (my table is 101 rows long). The plot that was generated was completely empty. What happened?
I'm guessing perhaps your data falls out of the ranges you have specified in xlim and ylim e.g.:
library(ggplot2)
df <- data.frame(x=1:10,y=1:10)
ggplot(a, aes(x=x, y=y)) +
geom_point(size=4) + xlab("Rank") +
ylab("Inter-helix angle (Degree)")
works fine, but:
ggplot(df, aes(x=x, y=y)) +
geom_point(size=4) + xlab("Rank") +
ylab("Inter-helix angle (Degree)") + xlim(0,0.1) + ylim(0,0.1)
gives the same error as you get because no values fall between 0 and 0.1
Warning message:
Removed 10 rows containing missing values (geom_point).
Although without your data this is just the most likely answer. By the way in general folk tend to use coord_cartesian:
ggplot(df, aes(x=x, y=y)) +
geom_point(size=4) + xlab("Rank") +
ylab("Inter-helix angle (Degree)") + coord_cartesian(xlim=c(0,0.1)) +
coord_cartesian(ylim=c(0,0.1))

Resources