tidyverse / ggplot2 Goto Github PK
View Code? Open in Web Editor NEWAn implementation of the Grammar of Graphics in R
Home Page: https://ggplot2.tidyverse.org
License: Other
An implementation of the Grammar of Graphics in R
Home Page: https://ggplot2.tidyverse.org
License: Other
mydata <- data.frame(x = 1:10, y = 1:10)
ggplot(mydata, aes(x, y)) +
geom_point() +
annotate("text", x = 6, y = 7, label = "beta[1] == 1", parse = T)
qplot(rlnorm(100), rlnorm(100)) +
coord_trans(x="log") +
scale_y_continuous(limits=c(0,10),expand=c(0,0))
In the following example, it expands the y-axis too much - it should change the aspect ratio instead.
qplot(mpg, wt, data = mtcars) + coord_equal()
That takes hue, chroma and luminance as inputs and outputs colour.
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
geom_rect(xmin = 15, xmax = 20, ymin = 3, ymax = 5)
vs.
ggplot() +
geom_point(aes(mpg, wt), data = mtcars) +
geom_rect(xmin = 15, xmax = 20, ymin = 3, ymax = 5)
a <- structure(list(date = structure(c(1227633710, 1227633725, 1227633740,
1227633756, 1227633771, 1227633786, 1227633802, 1227633818, 1227633833,
1227633848, 1227633863, 1227633878), class = c("POSIXt", "POSIXct"
), tzone = ""), x = c(10.2516422, 11.1053127, 11.6594761, 13.2709195,
14.0127093, 14.7598268, 14.6802017, 15.2590637, 15.5089719, 15.5101305,
15.1716798, 14.9050077), y = c(-11.5090033, -10.8087639, -10.0120475,
-7.9856268, -6.5551218, -5.1593914, -4.1750486, -2.3780065, -0.5245896,
1.3263083, 3.3031301, 4.5791333)), .Names = c("date", "x", "y"
), row.names = c(NA, -12L), class = "data.frame")
ggplot(a) + geom_point(aes(x=x, y=y, colour=date))
geom_contour generates errors and does not correctly draw contours for density_2d
qplot(rating, length, data = movies, geom = "density2d",
colour = factor(Comedy), ylim = c(0, 150))
xlim and ylim should be specified in the original data units, and use the scales to transform them into plotted units.
df <- data.frame(x = runif(100) * 360, y = runif(100))
base <- ggplot(df, aes(x, y)) + geom_point() + coord_polar()
base
base + scale_x_continuous(breaks = c(0, 180, 270))
To ensure that map aspect ratios are correct
Although in principle it should be fairly straightforward.
dummy <- data.frame(
x = rnorm(1000),
y = rnorm(1000),
z = gl(2, 500, labels = c("A rather long label", "An even longer
label than the first label"))
)
ggplot(dummy, aes(x = x, y = y, colour = z)) + geom_point()
last_plot() + theme_gray(base_size = 4)
Reported by Thierry Onkelinx
I'm not sure what to do here, but it should at least be documented.
p = qplot(runif(5),runif(5)) + opts(title="A dumb plot")
# this is the default
theme_set(theme_grey(12))
quartz(width=5, height=4, pointsize=12)
p
# this has display issues (overwriting in legends etc.) because the
# font size is too large compared to what the device "expects"
theme_set(theme_grey(25))
quartz(width=5, height=4, pointsize=8)
p
# when the pointsize is set accordingly, the proportions are
# "harmonious" again (well, it still does not look great that's because
#25 is much to big for this size of plot)
theme_set(theme_grey(25))
quartz(width=5, height=4, pointsize=25)
p
# Ahhh, that's better
theme_set(theme_grey(10))
quartz(width=5, height=4, pointsize=10)
p
df <- data.frame(x = runif(10) * 2 * pi)
base <- ggplot(df, aes(x, 1)) +
geom_point() +
ylim(0,1.1) +
coord_polar()
breaks <- seq(0, 2 * pi, pi/2)
base + scale_x_continuous(limits = range(breaks), breaks = breaks)
labels <- c("0", expression(frac(pi,2)), expression(pi),
expression(frac(3*pi,2)), expression(pi))
base + scale_x_continuous(limits = range(breaks),
breaks = breaks, labels = labels)
#0 point converted to string
In the following example, the groups should be displayed in alphabetical order: gain on the left and loss on the right.
df <- data.frame(
FRAME = factor(c("Loss", "Gain", "Loss", "Gain")),
Mean = c(5.28, 5.23, 4.95, 5.12),
FOCUS = factor(c("Prevention", "Prevention", "Promotion", "Promotion"))
)
dodge <- position_dodge(width=0.90)
ggplot(df, aes(FOCUS, Mean, order=FRAME, colour=FRAME)) +
geom_point(position=dodge, size = 4)
Because setting it is confusing.
qplot(mpg, wt, data = mtcars) + facet_grid(vs + am ~ .)
The following two plots are identical:
qplot(carat, price, data = diamonds, geom = "bin2d") +
scale_colour_gradient()
qplot(carat, price, data = diamonds, geom = "bin2d") +
scale_colour_gradient(trans = "log10")
Should be geom_line
+ stat_function
qplot(mpg, wt, data=mtcars) + geom_point(x = 0)
qplot(mpg, wt, data=mtcars) + geom_point(x = 20)
data <- data.frame(x = 1:10, y1 = 1:10, y2 = 1:10)
ggplot(data=data, aes(x=x)) + geom_smooth(aes(y=y1, linetype="y1"))
ggplot(data=data, aes(x=x)) + geom_smooth(aes(y=y1, color="y1"))
ggplot(data=data, aes(x=x)) + geom_smooth(aes(y=y1, color="a", linetype="b"))
In this example, points and lines are used for both values.
df.actual <- data.frame(x = 1:10, y = 1:10)
df.approx <- data.frame(x = 1:15, y = 1:15 + rnorm(15, sd = 3))
ggplot(df.actual, aes(x, y)) +
geom_point(aes(colour="Actual")) +
geom_line(aes(colour="Approximate"), data=df.approx)
da <- data.frame(
x = rep(1:5, 2),
y = c(1+(0:4), 1+(0:4)/10),
z = rep(1:2, each=5))
qplot(x, y, data=da, group=z, geom="line") +
facet_grid(z ~ ., scales="free_y", space="free")
Panel two is about half the size it should be
qplot(mpg, wt, data = mtcars) + facet_grid(vs ~ am, margins = "grand_col")
For example,
qplot(mpg, wt, data = mtcars) +
geom_point(data = data.frame(), colour = "red")
should display black points.
This can occur when subsetting and is annoying.
df <- data.frame(
FRAME = factor(c("Loss", "Gain", "Loss", "Gain")),
Mean = c(5.28, 5.23, 4.95, 5.12),
FOCUS = factor(c("Prevention", "Prevention", "Promotion", "Promotion"))
)
dodge <- position_dodge(width=0.90)
base <- ggplot(df, aes(FOCUS, Mean, fill=FRAME, colour=FRAME)) +
geom_point(position=dodge, size = 4)
# Do work:
base + geom_text(aes(label = Mean), position = dodge)
base + geom_bar(position=dodge, stat="identity")
# Don't work
base + geom_bar(position=dodge)
base + geom_boxplot(position=dodge)
By default, scale names should be stored in options. This will allow simplification of the current default scale production so that scales only need to be added when the plot is drawn, and fix the following bugs:
This would be useful for annotations, and for geom_vline and geom_hline. The main complication would be ensuring correct translation in coordinate systems.
ggplot(data.frame(x=0:1,y=0:1)) +
geom_rect(aes(x=x,y=y),xmin=.1,xmax=.2,ymin=-Inf,ymax=Inf)
Because of use of merge. Replace with join function from devel version of plyr.
If breaks is a named vector, the names should be used as labels.
x <- seq(-5,5,by=0.1)
d <- data.frame(x = x, y = dnorm(x))
base <- ggplot(d, aes(x, y)) + geom_path() + geom_point() + ylim(0, 0.4)
base + coord_polar()
base + coord_polar() + xlim(-1, 1)
To give control over low-level grid params.
For integers with limited range and ordered factors, ggplot2 should use ordinal scales. In particular, colour should default to an sequential brewer palette, and shape should use shapes with increasing number of edges.
myd <- data.frame(
myvar = c(1,5,10,50,100,500,1000,5000,10000),
myvarb = c(1:9)
)
base <- ggplot(myd, aes(myvarb, myvar))+
geom_point()+
geom_abline(intercept = 1, slope = 100)
base + scale_y_log10()
e.g. scale_x_continuous(breaks = 1:4, labels = 1:2)
Exercise 11 of chapter 4 of Simon Woods' GAM book - having obtained the model matrix and penalty for the smooth from the object returned by e.g. smooth.construct2(s(x,bs="cr"),data=list(x=x),knots=NULL)
Should be faster for the special case needed for stat_smooth
.
library(maps)
data <- data.frame(
lat = c(-41,-42,-41,-42),
lon = c(170,170,171,171),
var = c(1,2,3,4))
coast <- data.frame(map("nz", plot=FALSE)[c("x","y")])
plot <- ggplot(data,aes(lon, lat)) +
geom_tile(aes(fill = var)) +
geom_polygon(data = coast,aes(x, y))
plot #OK
plot + coord_equal() #OK
plot + coord_map()
set.seed(1)
d = data.frame(x=1:10, y=runif(10), z=runif(10))
ggplot(d) + geom_point(aes(x,y, size=z, colour=z)) + scale_area()
# fails
set.seed(7)
d = data.frame(x=1:10, y=runif(10), z=runif(10))
ggplot(d) + geom_point(aes(x,y, size=z, colour=z)) + scale_area()
# works
qplot(mpg, wt, data = mtcars) + geom_vline(xintercept = 20) + coord_flip()
qplot(mpg, wt, data = mtcars) + geom_hline(yintercept = 5) + coord_flip()
df <- data.frame(a = 2 ^ (1:10), b = 3 ^ (1:10))
myplot <- ggplot(df, aes(a,b)) +
geom_line() +
scale_x_log10() + scale_y_log10()
myplot
myplot + coord_equal()
This makes it impossible to set defaults set_default_scale("shape","discrete", "manual",value=c(1))
and adds ugly special case code to Scales$add_defaults
.
Create stat for computing ecdf. Work with geom_step to create plot of ecdf.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.