Using facet_trelliscope from trelliscopejs this works:
library(trelliscopejs)
library(ggplot2)
x <- 1:10
y <- 1:10
group <- rep(c("A","B"),5)
df <- data.frame(group, x, y)
ggplot(df, aes(x, y)) + geom_point() +
xlim(0, 10) + ylim(0, 10) +
facet_trelliscope(~group)
But this doesn't:
ggplot(df, aes(x, y)) + geom_point() +
facet_trelliscope(~group)
It throws up this error:
Error in scale_fn() : could not find function "scale_fn"
My question is, do axis limits always need to be defined for facet_trelliscope to work?
Thanks both it was indeed a package error. If anyone sees the error in future just remove and re-install ggplot2.
Related
Is there an easy way to scale a ggplot by log base 20 or higher? This works great:
ggplot(data, aes(x, y)) + geom_line() + scale_x_log10()
Unfortunately base 10 too small. There's a more general scale_x_continuous function that takes a trans argument, but there doesn't appear to be any log transforms higher than base 10.
Here's a worked example of creating a new trans object to use in your plot:
Initial plot
library(ggplot2)
df <- data.frame(x = 1:10, y = 10^(1:10))
p <- ggplot(df, aes(x, y)) + geom_line()
p
With log scale using base 100
p + scale_y_continuous(trans = scales::trans_new(name = "log100",
transform = function(x) log(x, 100),
inverse = function(x) 100^x,
domain = c(1e-16, Inf)),
breaks = scales::breaks_log(5, 100),
labels = scales::comma)
Created on 2020-12-07 by the reprex package (v0.3.0)
The scales package provides transforms to the scale_x_continuous() function. You can either use the built-in flexible log transform or create your own using the trans_new() function.
Built-in with base-20:
require(scales)
base=20
p1 <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
p1 + scale_y_continuous(trans = scales::log_trans(base))
Make your own transform:
require(scales)
logTrans <- function(base=20){
trans_new(name='logT',
transform=function(y){
return(log(y, base=base))
},
inverse=function(y){
return(base^y)
})
}
base=20
p1 + scale_y_p1 <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
p1 + continuous(trans = logTrans(base=base))
I would like to draw a ggplot with a random theme (In fact, I want to draw many plots, each with a different theme). Consider the following reproducible example:
# Exmple data
df <- data.frame(x = 1:10, y = 1:10)
# Select theme randomly
random_theme <<- sample(c("theme_gray", "theme_bw", "theme_light", "theme_dark", "theme_minimal", "theme_classic"), 1)
# Draw ggplot
ggplot(df, aes(x, y)) +
geom_line() +
random_theme # This line does not work
Question: How can I select a ggtheme randomly?
Sample from the functions and not the names of the functions. Also, sample returns a list when sampling from anything more complex than a scalar, so you need the first list element. Eg:
> sample(c(sqrt, sqrt),2)
[[1]]
function (x) .Primitive("sqrt")
[[2]]
function (x) .Primitive("sqrt")
So get a random theme function with:
random_theme <- sample(c(theme_gray, theme_bw, theme_light, theme_dark, theme_minimal, theme_classic), 1)[[1]]
and call it when you plot:
ggplot(df, aes(x, y)) +geom_line() + random_theme()
Resample random_theme and plot again to update.
Also, you probably don't need the <<- which I guess is a hangover from desperately trying to make something work...
You could do this with match.fun():
random_theme = match.fun(sample(c("theme_gray", "theme_bw", "theme_light", "theme_dark", "theme_minimal", "theme_classic"), 1))
ggplot(df, aes(x, y)) +
geom_line() +
random_theme()
Sice your random_theme is a character vector, you can use eval and then parse to parse your theme.
library(tidyverse)
ggplot(df, aes(x, y)) +
geom_line() +
eval(parse(text = paste0(random_theme, "()")))
Or more directly:
ggplot(df, aes(x, y)) +
geom_line() +
eval(parse(text = paste0(sample(c("theme_gray",
"theme_bw",
"theme_light",
"theme_dark",
"theme_minimal",
"theme_classic"), 1) , "()")))
Is there any way to set the break step size in ggplot without defining a sequence. For example:
x <- 1:10
y <- 1:10
df <- data.frame(x, y)
# Plot with auto scale
ggplot(df, aes(x,y)) + geom_point()
# Plot with breaks defined by sequence
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(breaks = seq(0,10,1))
# Plot with automatic sequence for breaks
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(breaks = seq(min(df$y),max(df$y),1))
# Does this exist?
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(break_step = 1)
You may say I am being lazy but there have been a few occasions where I have had to change the min and max limits of my seq due to the addition of error bars. So I just want to say...use a break size of x, with automatic scale limits.
You can define your own function to pass to the breaks argument. An example that would work in your case would be
f <- function(y) seq(floor(min(y)), ceiling(max(y)))
Then
ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f)
gives
You could modify this to pass the step of the breaks, e.g.
f <- function(k) {
step <- k
function(y) seq(floor(min(y)), ceiling(max(y)), by = step)
}
then
ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f(2))
would create a y-axis with ticks at 2, 4, .., 10, etc.
You can take this even further by writing your own scale function
my_scale <- function(step = 1, ...) scale_y_continuous(breaks = f(step), ...)
and just call it like
ggplot(df, aes(x,y)) + geom_point() + my_scale()
> # Does this exist?
> ggplot(df, aes(x,y)) + geom_point() +
> scale_y_continuous(break_step = 1)
If you're looking for an off-the-shelf solution, then you can use the scales::breaks_width() function like so:
scale_y_continuous(breaks = scales::breaks_width(1))
The scales package also includes handy functions to control breaks easily in "special" scales such as date-time, e.g. scale_x_datetime(breaks='6 hours').
I'm an R/ggplot newbie switching over from MatLab.
I would like to create a function using ggplot with linear regression equation printed on the graph (which is discussed in Adding Regression Line Equation and R2 on graph). But here, I am trying to build a function with it but wasn't successful.
I kept getting an error -
"Error in eval(expr, envir, enclos) : object 'label' not found".
One workaround is to define "label" variable outside of the function but I just don't understand why this doesn't work.
Can anyone explain why?
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
f <- function(DS, x, y, z) {
label <- z
print(label)
ggplot(DS, aes(x=x, y=y)) +
geom_point() +
labs(y=y) +
labs(title=y) +
xlim(0,5)+
ylim(0,5)+
geom_smooth(method="lm", se=FALSE)+
geom_text (aes(x=1, y=4, label=label))
}
f(df, x, y, "aaa") #execution line
See the following code:
library(ggplot2)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
f <- function(DS, x, y, z) {
label.df = data.frame(x=1, y=4, label=z)
ggplot(DS, aes_string(x=x, y=y)) +
geom_point() +
labs(y=y) +
labs(title=y) +
geom_smooth(method="lm", se=FALSE)+
geom_text (aes(x=x, y=y, label=label), label.df)
}
f(df, "x", "y", "aaa")
There were a few fixes about your code:
The data you are using in geom_text is the same you have defined in ggplot() unless you change it. Here I have created a temporary data.frame for this purpose called label.df.
The xlim() and ylim() functions were filtering most of your data points, since the range of x and y are much larger than the limits you defined in the original code.
When you want to pass the names of the columns of your data.frame to be used for displaying the graph it would be easier to pass their names as strings (i.e. "x"). This way, the aes() function is also changed to aes_string().
Here is the result:
Edit
Thanks to #Gregor, a simpler version would be:
f <- function(DS, x, y, z) {
ggplot(DS, aes_string(x=x, y=y)) +
geom_point() +
labs(y=y) +
labs(title=y) +
geom_smooth(method="lm", se=FALSE)+
annotate(geom="text", x=1, y=4, label=z)
}
I have a 2d hexagon density plot with many points. I would like the counts within the hexagons to be displayed on a logarithmic scale, but I can't figure out how to do this through ggplot2.
Here is a simple example:
x <- runif(1000, 50, 100)
y <- rnorm(1000, mean = 10, sd = 8)
df <- as.data.frame(cbind(x, y))
ggplot(df, aes(x, y)) + stat_binhex()
Another way is to pass trans = argument to scale_fill_* used.
ggplot(df, aes(x, y)) + geom_hex() + scale_fill_gradient(trans = "log")
ggplot(df, aes(x, y)) + geom_hex() + scale_fill_viridis_c(trans = "log")
There is a fill aesthetics that defaults to ..count.. when you do not specify it in stat_binhex. The code below produces same plot as your original code.
ggplot(df, aes(x, y)) + stat_binhex(aes(fill=..count..))
If you want to have a log scale for counts, then the solution is pretty straight forward:
ggplot(df, aes(x, y)) + stat_binhex(aes(fill=log(..count..)))