Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create templates using ggplot2 syntax?

Tags:

r

ggplot2

I wonder if it's possible to create a similar set of figures in ggplot2 and just change the data somehow. For instance, I can create a function to accomplish this task:

plot1 <- function(data) ggplot(data) + geom_line(aes(x,y)) + theme_bw()
plot1(data)
plot1(newdata)

But is it possible to save and reuse a set of components in some manner like this? (obviously this does not work):

g <- geom_line(aes(x,y)) + theme_bw()
ggplot(data) + g
ggplot(newdata) + g
like image 302
hatmatrix Avatar asked May 24 '13 01:05

hatmatrix


People also ask

What is the difference between ggplot and ggplot2?

You may notice that we sometimes reference 'ggplot2' and sometimes 'ggplot'. To clarify, 'ggplot2' is the name of the most recent version of the package. However, any time we call the function itself, it's just called 'ggplot'.

What does Geom_point mean?

geom_point.Rd. The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.

What is the use of ggplot2 package in R?

ggplot2 is a R package dedicated to data visualization. It can greatly improve the quality and aesthetics of your graphics, and will make you much more efficient in creating them. ggplot2 allows to build almost any type of chart.

Can I use ggplot in Python?

Using ggplot in Python allows you to build visualizations incrementally, first focusing on your data and then adding and tuning components to improve its graphical representation. In the next section, you'll learn how to use colors and how to export your visualizations.


1 Answers

There are the +.gg methods described here

These are %+% and %+replace% which will update / replace elements in ggplots and themes

eg

p <- ggplot(mtcars, aes(x =wt, y = mpg,colour = hp)) + geom_point()

# change the variable mapped to y
p %+% aes(y = am)
# change the data set
p %+% mtcars[1:10,]

Or you can combine the elements as a list

eg

#
g <- list(geom_line(aes(x,y)),theme_bw())
ggplot(data) + g
like image 122
mnel Avatar answered Oct 02 '22 15:10

mnel