Please see attached plots using the standard plot() function vs ggplot() function.
I am currently playing around with theme() to try and replicate the plot() background/panels and general look etc. but I can't get it quite right.
I've tried:
theme_update(panel.background = element_rect(fill = "white", colour = "grey50"))
But it's not quite what I'm looking for and it reverts back to the gray background whenever I relaunch RStudio....
Any helpful pointers would be greatly appreciated.
Thanks
Following my comment I provide a reproducible example using diamonds dataset:
library(ggplot2)
library(ggthemes)
set.seed(100)
mydata <- diamonds[sample(nrow(diamonds), 100), ]
ggplot(data = mydata, aes(x = carat, y = price)) +
geom_point(shape = 1) +
theme_base()
Related
Hi I'm trying to save high quality (300 dpi) images using RStudio but no luck so far. I've looked around a lot on the internet but no answer seems to work. Even when I run the code below, no file shows up on my computer. Any help is appreciated!
install.packages("gapminder")
library(gapminder)
data("gapminder")
attach(gapminder)
plot(lifeExp ~ log(gdpPercap))
ggsave("filename.png",dpi = 300)
It works fine if you use ggplot() from ggplot2 instead of plot()
Packages and data
library(ggplot2)
library(gapminder)
data("gapminder")
attach(gapminder)
Solution
ggplot(gapminder,
aes(x = log(gdpPercap), y = lifeExp)) +
geom_point()
ggsave("filename.png",dpi = 300)
Here are some tweaks you came make to make it more similar to plot() appearance:
ggplot(gapminder,
aes(x = log(gdpPercap), y = lifeExp)) +
geom_point(shape = 1) +
theme_linedraw()
output from last code
I'm trying to create an image similar to this one in R using ggplot2.
However, I'm new to using this package. I'm struggling to find out how to draw lines that each have a different gradient. I want each line to start with one colour and end in another colour (gradually changing throughout), and I want to be able to specify this for each individual line uniquely. Can I do this with geom_segment? Would it also be possible for curves with geom_curve? It seems that the package ggforce could be useful for this. Any help would be greatly appreciated! Thank you.
This is the best I could pull together in 20 minutes to just illustrate that ggforce can be handy.
library(ggplot2)
library(ggforce)
n <- 1000
df <- data.frame(
x = runif(2 * n),
id = rep(seq_len(n), each = 2),
y = rep(c(0:1), n)
)
g <- ggplot(df, aes(x = x, y = y)) +
geom_link2(aes(group = id, colour = x),
alpha = 0.3) +
scale_colour_gradientn(colours = rainbow(100),
guide = "none") +
theme_void() +
theme(plot.background = element_rect(fill = "black"))
I'm trying to find an easy and intuitive way to calculate and display the peaks of a ggplot2::geom_density() object.
This blog explains how to do it in base R, but it is a multistep process.
But it seems much more intuitive to use the stat_peaks() function of the ggpmisc package.
However, when running the code below, I get the error: stat_peaks requires the following missing aesthetics: y
library(tidyverse)
library(ggpmisc)
ggplot(iris, aes(x = Petal.Length)) +
geom_density() +
stat_peaks(colour = "red")
When creating a geom_density() you don't need to supply a y aesthetic.
So if indeed stat_peaks is the way to go, is there a work around to this issue? Perhaps there is a better solution to my problem.
Here is a simple workaround. The idea is to call ggplot_build, let ggplot do the calculations for you and then extract the needed y aesthetic from the resulting object, which is density in your case.
library(ggplot2)
library(ggpmisc)
p <- ggplot(iris, aes(x = Petal.Length)) +
geom_density()
pb <- ggplot_build(p)
p + stat_peaks(
data = pb[['data']][[1]], # take a look at this object
aes(x = x, y = density),
colour = "red",
size = 3
)
I'm sure that this approach can be improved by one of the ggplot2 wizards around that can explain why this is not working...
ggplot(iris, aes(x = Petal.Length, y = stat(density))) +
geom_density() +
stat_peaks()
error: stat_peaks requires the following missing aesthetics: y
... which was my first guess.
I don't use ggplot2 that much, but today I thought I'd give it a go on some graphs. But I can't figure out how to manually control colors in geom_line()
I'm sure I'm overlooking something simple, but here's my test code:
x <- c(1:20, 1:20)
variable <- c(rep("y1", 20), rep("y2", 20) )
value <- c(rnorm(20), rnorm(20,.5) )
df <- data.frame(x, variable, value )
d <- ggplot(df, aes(x=x, y=value, group=variable, colour=variable ) ) +
geom_line(size=2)
d
which gives me the expected output:
I thought all I had to do was something simple like:
d + scale_fill_manual(values=c("#CC6666", "#9999CC"))
But that changes nothing. What am I missing?
color and fill are separate aesthetics. Since you want to modify the color you need to use the corresponding scale:
d + scale_color_manual(values=c("#CC6666", "#9999CC"))
is what you want.
Here's a minimal reproducible example of another way to change line colours (try running it):
library(tidyverse)
library(ggplot2)
iris %>%
ggplot(aes(x = Petal.Length)) +
geom_line(aes(y = Sepal.Length), color = "green") +
geom_line(aes(y = Sepal.Width), color = "blue")
This way can be particularly useful when you added the lines manually.
Does anyone know how to create a scatterplot in R to create plots like these in PRISM's graphpad:
I tried using boxplots but they don't display the data the way I want it. These column scatterplots that graphpad can generate show the data better for me.
Any suggestions would be appreciated.
As #smillig mentioned, you can achieve this using ggplot2. The code below reproduces the plot that you are after pretty well - warning it is quite tricky. First load the ggplot2 package and generate some data:
library(ggplot2)
dd = data.frame(values=runif(21), type = c("Control", "Treated", "Treated + A"))
Next change the default theme:
theme_set(theme_bw())
Now we build the plot.
Construct a base object - nothing is plotted:
g = ggplot(dd, aes(type, values))
Add on the points: adjust the default jitter and change glyph according to type:
g = g + geom_jitter(aes(pch=type), position=position_jitter(width=0.1))
Add on the "box": calculate where the box ends. In this case, I've chosen the average value. If you don't want the box, just omit this step.
g = g + stat_summary(fun.y = function(i) mean(i),
geom="bar", fill="white", colour="black")
Add on some error bars: calculate the upper/lower bounds and adjust the bar width:
g = g + stat_summary(
fun.ymax=function(i) mean(i) + qt(0.975, length(i))*sd(i)/length(i),
fun.ymin=function(i) mean(i) - qt(0.975, length(i)) *sd(i)/length(i),
geom="errorbar", width=0.2)
Display the plot
g
In my R code above I used stat_summary to calculate the values needed on the fly. You could also create separate data frames and use geom_errorbar and geom_bar.
To use base R, have a look at my answer to this question.
If you don't mind using the ggplot2 package, there's an easy way to make similar graphics with geom_boxplot and geom_jitter. Using the mtcars example data:
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot() + geom_jitter() + theme_bw()
which produces the following graphic:
The documentation can be seen here: http://had.co.nz/ggplot2/geom_boxplot.html
I recently faced the same problem and found my own solution, using ggplot2.
As an example, I created a subset of the chickwts dataset.
library(ggplot2)
library(dplyr)
data(chickwts)
Dataset <- chickwts %>%
filter(feed == "sunflower" | feed == "soybean")
Since in geom_dotplot() is not possible to change the dots to symbols, I used the geom_jitter() as follow:
Dataset %>%
ggplot(aes(feed, weight, fill = feed)) +
geom_jitter(aes(shape = feed, col = feed), size = 2.5, width = 0.1)+
stat_summary(fun = mean, geom = "crossbar", width = 0.7,
col = c("#9E0142","#3288BD")) +
scale_fill_manual(values = c("#9E0142","#3288BD")) +
scale_colour_manual(values = c("#9E0142","#3288BD")) +
theme_bw()
This is the final plot:
For more details, you can have a look at this post:
http://withheadintheclouds1.blogspot.com/2021/04/building-dot-plot-in-r-similar-to-those.html?m=1