Scale value inside of aes_string()
You can use tidyeval
approach introduced in ggplot2 v3.0.0
# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
var1 <- "wt"
var2 <- "mpg"
multiplier <- 10
ggplot(data = subset(mtcars, cyl == 4),
aes(x = !! rlang::sym(var1), y = !! rlang::sym(var2))) +
geom_line(size = 1.5, color = "#00868B") +
geom_line(data = subset(mtcars, cyl == 8),
aes(x = !! rlang::sym(var1), y = !! rlang::sym(var2) * multiplier))
Or put everything in a function
plot_select_vars <- function(var1, var2, multiplier) {
var1 <- rlang::sym(var1)
var2 <- rlang::sym(var2)
ggplot(data = subset(mtcars, cyl == 4),
aes(x = !! var1, y = !! var2)) +
geom_line(size = 1.5, color = "#00868B") +
geom_line(data = subset(mtcars, cyl == 8),
aes(x = !! var1, y = !! var2 * multiplier))
}
plot_select_vars(var1, var2, multiplier)
Created on 2018-06-06 by the reprex package (v0.2.0).
Use variables supplied to aes_string() in other ggplot functions
You can extract the y
variable from your data inside the expand_limits
and scale that by 5%:
expand_limits(y = c(0, max(data[, var])*1.05))
Which makes:
fig_fun <- function(data, var){
ggplot(data, aes_string(x = "wt", y = var)) +
geom_line() +
geom_point() +
expand_limits(y = c(0,max(data[, var])*1.05))) # picking the var column here
}
You will need an additional + scale_y_continuous(expand = c(0, 0))
to absolutely limit to those numbers.
But as others suggested, if you use the default value for expand
parameter of scale_y_continuous
you'd get what you want.
So scale_y_continuous(expand = c(0, 0.1))
would give you 10% extra space from your y axis boundaries on either side. Docs are here.
The expand_limits
method is still useful if you want some more custom solutions.
How do I write a function for a plot in ggplot2 using correctly aes?
Use aes_string
instead of aes
. It should work. Worked for me :)
Note: Remove the quotes around your arguments in the function definition. Also your cor6 should be in quotes. See below
test <- function(explanatory,response)
{
plot<- ggplot(d, aes_string(x =explanatory, y=response)) +
geom_point()+
ggtitle("Correlation between Fertilizer and Yield") +
theme(plot.title = element_text(size = 10, face = "bold"))+
geom_smooth(method=lm, se=FALSE) +
annotate("text", x=800, y=20, size=5,label= "cor6")
plot
}
ggplot2 + aes_string inside a function via formula interface
You can use as.formula
and paste
:
p <- p + facet_grid(as.formula(paste(". ~", fac2)))
In your example, this gives:
What is the difference between aes and aes_string (ggplot2) in R
aes
saves you some typing as you don't need the quotes. That is all. You are of course free to always use aes_string
. You should use aes_string
if you want to pass the variable names programmatically.
Internally aes
uses match.call
for the non-standard evaluation. Here is a simple example for illustration:
fun <- function(x, y) as.list(match.call())
str(fun(a, b))
#List of 3
# $ : symbol fun
# $ x: symbol a
# $ y: symbol b
For comparison:
library(ggplot2)
str(aes(x = a, y = b))
#List of 2
# $ x: symbol a
# $ y: symbol b
The symbols are evaluated at a later stage.
aes_string
uses parse
to achieve the same:
str(aes_string(x = "a", y = "b"))
#List of 2
# $ x: symbol a
# $ y: symbol b
How do I combine aes() and aes_string() options
If you want to specify a character literal value in aes_string()
, the easiest thing would be to use shQuote()
to quote the value. For example
ggplot(mtcars, aes(x=wt)) + ylab("") +
geom_line(aes_string(y=v1, color=shQuote("one"))) +
geom_line(aes_string(y=v2, color=shQuote("two"))) +
scale_color_manual(name="Val", values=c(one="#105B63",two="#BD4932"))
This works because aes_string()
actually runs parse(text=)
on each of the parameter values. The shQuote()
function adds quotes around your character value so that when you parse the value, you get a character value back rather than a symbol/name. Compare these two calls
class(parse(text="a")[[1]])
# [1] "name"
class(parse(text=shQuote("a"))[[1]])
# [1] "character"
Alternatively, you might think of merging aes()
directives. The aes()
functions really just return a list with a class of uneval
. We could define a function to combine/merge these list. For example we can define addition
`+.uneval` <- function(a,b) {
`class<-`(modifyList(a,b), "uneval")
}
Now we can do
ggplot(mtcars, aes(x=wt)) + ylab("") +
geom_line(aes_string(y=v1) + aes(color="one")) +
geom_line(aes_string(y=v2) + aes(color="two")) +
scale_color_manual(name="Val", values=c(one="#105B63",two="#BD4932"))
Using aes_string to change color of geom_point from dynamic column
My opinion is to do your standard evaluation that allows for the dynamic functionality before you go into ggplot2 functions.
The below takes advantage of Standard evaluation versions of dplyr functions. It creates a column in the data frame dynamically called formatCol and bases the colour scale on it.
data.df <- data.frame(x = c(2, 1, 2),
y = c(3, 2, 4),
z = c(4, 3, 3),
label = c(1, 2, 3),
label2 = c(1, 2, 2))
library(ggplot2)
library(dplyr)
library(lazyeval)
formatCol <- names(data.df)[ncol(data.df)]
formula <- interp(~ifelse((label == 2), "a", "b"), label = as.name(formatCol))
plot.df <- data.df %>% mutate_(formatCol = formula)
g <- ggplot(plot.df, aes(x= x, y= y)) +
geom_point( aes(color = formatCol))+
scale_colour_manual(values= c("a" = "blue", "b" = "green"))
g
Why cant i use vector in scale fill manual?
You haven't supplied a reproducible example, but as Gregor Thomas points out, this just isn't how you create named vectors in R. Here's an example using the built-in iris
dataset that shows how you could use your existing variables to get the intended results.
Firstly, I'll assign the group names and colour names to match your set-up:
Group1 <- "setosa"
Group2 <- "versicolor"
Group3 <- "virginica"
Group1col <- "red"
Group2col <- "green"
Group3col <- "blue"
To easily create a named vector, we can use setNames
:
setNames(c(Group1col, Group2col, Group3col), c(Group1, Group2, Group3))
#> setosa versicolor virginica
#> "red" "green" "blue"
So to use the named variables in your plot, you could do:
ggplot(iris, aes(Sepal.Width, Petal.Length, colour = Species)) +
geom_point() +
scale_color_manual(values = setNames(c(Group1col, Group2col, Group3col),
c(Group1, Group2, Group3)))
Related Topics
"Non-Finite Function Value" When Using Integrate() in R
Generating a Date from a String with a 'Month-Year' Format
Selecting Unique Rows in Matrix Using R
Manual Simulation of Markov Chain in R
Extract Only Quarter from a Date in R
Modify Spacing Between Key Glyphs in Vertical Legend Whilst Keeping Key Glyph Border
Simulate an Ar(1) Process with Uniform Innovations
Sorting List of List of Elements of a Custom Class in R
Converting Multiple Boolean Columns to Single Factor Column
Group Values by Unique Elements
Plot.Lm Error: $ Operator Is Invalid for Atomic Vectors
How to Force the X-Axis Tick Marks to Appear at the End of Bar in Heatmap Graph
Efficient Way to Fill Time-Series Per Group
Using Predict() and Table() in R
Why Does "Hello" > 0 Return True
R Ggplot2 Using Italics and Non-Italics in the Same Category Label
Preventing Incosistent Spacing/Bar Widths in Geom_Bar with Many Bars
Cumulative Number of Unique Values in a Column Up to Current Row