I am creating a plot in ggplot2 with filled densities, a few of which I would like to truncate. I apologize for lack of images--apparently I'm not allowed to post them yet. A simple example of beginning code:
dd = with(density(rnorm(100,0,1)),data.frame(x,y))
ylimit = .3
ggplot(data = dd, mapping = aes(x = x, y = y), geom="line") +
layer(data = dd, mapping = aes(x = x, y = y), geom = "area",
geom_params=list(fill="red",alpha=.3)) +
scale_x_continuous(limits = c(-3,3)) +
scale_y_continuous(limits = c(0,ylimit))
This, however, results in an empty area in the middle of the filled density where dd$y > ylimit.
If I compensate for this with
dd$y = pmin(dd$y, ylimit)
The area is shaded but the plot displays an area slightly higher than ylimit, so the fill does not extend to the top of the graph.
Ideally I would like to know how to get ggplot display a plot exactly up to ylimit, but any other solutions for having the fill extend to the top of the plot would be welcome.
Edit:fixed the code.
I think this is what you meant. Note the use of ifelse to get the truncating behavior.
dd = with(density(rnorm(100,0,1)), data.frame(x, y))
ylimit = .3
dev.new(width=4, height=4)
ggplot(data = dd, mapping = aes(x = x, y = y), geom="line") +
layer(data = dd, mapping = aes(x = x, y = ifelse(y>ylimit, ylimit, y)), geom = "area",
geom_params=list(fill="red",alpha=.3)) +
scale_x_continuous(limits = c(-3,3)) +
coord_cartesian(ylim=c(0, ylimit))
Related
I was wondering how I can scale geom_hex not on count, but rather by a variable and heat scale it? I am also having overfitting in my actual model and was wondering how to eliminate that? Here's an examples:
'''
ggplot(data = diamonds)+
geom_hex(mapping = aes(x = x, y = price, fill = depth, bins =
25))+
scale_fill_continuous(type = "viridis")
'''
Thanks!
I think this will do the trick, assuming you want to colour the hexagons according to the mean of depth...
ggplot(diamonds, aes(x = x, y = price, z = depth)) +
stat_summary_hex(fun = mean, bins = 25) +
scale_fill_continuous(type = "viridis")
How can I make a confidence interval band that extends to the end of the plot in ggplot?
I can do it if the plotted band is entirely within the plot, for example
limits <- c(1e2, 1e7)
confPolygon <- tibble(
x = c(limits[1], limits[1]*10, limits[2], limits[2], limits[2]/10, limits[1], limits[1]),
y = c(limits[1], limits[1], limits[2]/10, limits[2], limits[2], limits[1]*10, limits[1])
)
plot <- ggplot() +
geom_polygon(data = confPolygon, aes(x = x, y = y), fill = "grey", alpha = .25) +
scale_x_log10(limits = limits) +
scale_y_log10(limits = limits)
works. However, if I try any shape that extends the polygon to the edges
confPolygon <- tibble(
x = c(limits[1], limits[2]*10, limits[2]*10, limits[1], limits[1]),
y = c(limits[1], limits[1], limits[2]*10, limits[2]*10, limits[1])
)
then it doesn't plot the polygon.
The reason is because the method you are using to zoom in to the plot (setting limits within the x or y scales) isn't meant to zoom in; it actually subsets the data, accidentally creating missing values on the way. Use coord_cartesian(xlim = c(0,5), ylim = c(0,5)), or in your case, coord_cartesian(xlim = limits, ylim = limits) instead, as this step does not subset the data.
One way to do this is with oob=scales::squish().
plot2 <- ggplot() +
geom_polygon(data = confPolygon, aes(x = x, y = y), fill = "grey", alpha = .25) +
scale_x_log10(limits = limits, oob=scales::squish) +
scale_y_log10(limits = limits, oob=scales::squish)
If you really want the polygon to extend all the way to the edge, you should also add expand=c(0,0) to each of the scale_*_log10() argument lists.
I am trying to generate a ternary plot using ggtern.
My data ranges from 0 - 1000 for x, y,and z variables. I wondered if it is possible to extend the axis length above 100 to represent my data.
#Nevrome is on the right path, your points will still be plotted as 'compositions', ie, concentrations sum to unity, but you can change the labels of the axes, to indicate a range from 0 to 1000.
library(ggtern)
set.seed(1)
df = data.frame(x = runif(10)*1000,
y = runif(10)*1000,
z = runif(10)*1000)
breaks = seq(0,1,by=0.2)
ggtern(data = df, aes(x, y, z)) +
geom_point() +
limit_tern(breaks=breaks,labels=1000*breaks)
I think there is no direct solution to do this with ggtern. But an easy workaround could look like this:
library(ggtern)
df = data.frame(x = runif(50)*1000,
y = runif(50)*1000,
z = runif(50)*1000,
Group = as.factor(round(runif(50,1,2))))
ggtern() +
geom_point(data = df, aes(x/10, y/10, z/10, color = Group)) +
labs(x="X", y="Y", z="Z", title="Title") +
scale_T_continuous(breaks = seq(0,1,0.2), labels = 1000*seq(0,1,0.2)) +
scale_L_continuous(breaks = seq(0,1,0.2), labels = 1000*seq(0,1,0.2)) +
scale_R_continuous(breaks = seq(0,1,0.2), labels = 1000*seq(0,1,0.2))
I'd like to annotate all y-values greater than a y-threshold using ggplot2.
When you plot(lm(y~x)), using the base package, the second graph that pops up automatically is Residuals vs Fitted, the third is qqplot, and the fourth is Scale-location. Each of these automatically label your extreme Y values by listing their corresponding X value as an adjacent annotation. I'm looking for something like this.
What's the best way to achieve this base-default behavior using ggplot2?
Updated scale_size_area() in place of scale_area()
You might be able to take something from this to suit your needs.
library(ggplot2)
#Some data
df <- data.frame(x = round(runif(100), 2), y = round(runif(100), 2))
m1 <- lm(y ~ x, data = df)
df.fortified = fortify(m1)
names(df.fortified) # Names for the variables containing residuals and derived qquantities
# Select extreme values
df.fortified$extreme = ifelse(abs(df.fortified$`.stdresid`) > 1.5, 1, 0)
# Based on examples on page 173 in Wickham's ggplot2 book
plot = ggplot(data = df.fortified, aes(x = x, y = .stdresid)) +
geom_point() +
geom_text(data = df.fortified[df.fortified$extreme == 1, ],
aes(label = x, x = x, y = .stdresid), size = 3, hjust = -.3)
plot
plot1 = ggplot(data = df.fortified, aes(x = .fitted, y = .resid)) +
geom_point() + geom_smooth(se = F)
plot2 = ggplot(data = df.fortified, aes(x = .fitted, y = .resid, size = .cooksd)) +
geom_point() + scale_size_area("Cook's distance") + geom_smooth(se = FALSE, show_guide = FALSE)
library(gridExtra)
grid.arrange(plot1, plot2)
I'm using ggplot2 to show lines and points on a plot. What I am trying to do is to have the lines all the same color, and then to show the points colored by an attribute. My code is as follows:
# Data frame
dfDemo <- structure(list(Y = c(0.906231077471568, 0.569073561538186,
0.0783433165521566, 0.724580209473378, 0.359136092118470, 0.871301974471722,
0.400628333618918, 1.41778205350433, 0.932081770977729, 0.198188442350644
), X = c(0.208755495088456, 0.147750173706688, 0.0205864576474412,
0.162635017485883, 0.118877260137735, 0.186538613831806, 0.137831912094464,
0.293293029083812, 0.219247919537514, 0.0323148791663826), Z = c(11112951L,
11713300L, 14331476L, 11539301L, 12233602L, 15764099L, 10191778L,
12070774L, 11836422L, 15148685L)), .Names = c("Y", "X", "Z"
), row.names = c(NA, 10L), class = "data.frame")
# Variables
X = array(0.1,100)
Y = seq(length=100, from=0, by=0.01)
# make data frame
dfAll <- data.frame()
# make data frames using loop
for (x in c(1:10)){
# spacemate calc
Floors = array(x,100)
# include label
Label = paste(' ', toString(x), sep="")
df1 <- data.frame(X = X * x, Y = Y, Label)
# merge df1 to cumulative df, dfAll
dfAll <- rbind(dfAll, df1)
}
# plot
pl <- ggplot(dfAll, aes(x = X, y = Y, group = Label, colour = 'Measures')) + geom_line()
# add points to plot
pl + geom_point(data=dfDemo, aes(x = X, y = Y)) + opts(legend.position = "none")
This almost works, but I am unable to color the points by Z when I do this. I can plot the points separately, colored by Z using the following code:
ggplot(dfDemo, aes(x = X, y = Y, colour = Z)) + geom_point()
However, if I use the similar code after plotting the lines:
pl + geom_point(data=dfDemo, aes(x = X, y = Y, colour = Z)) + opts(legend.position = "none")
I get the following error:
Error: Continuous variable () supplied to discrete scale_hue.
I don't understand how to add the points to the chart so that I can colour them by a value. I appreciate any suggestion how to solve this.
The issue is that they are colliding the two colour scales, one from the ggplot call and the other from geom_point. If you want the lines of one colour and the points of different colours then you need to erase the colour setting from ggplot call and put it inside the geom_line outside the aes call so it isn't mapped. Use I() to define the colour otherwise it will think is just a variable.
pl <- ggplot(dfAll, aes(x = X, y = Y, group = Label)) +
geom_line(colour = I("red"))
pl + geom_point(data=dfDemo, aes(x = X, y = Y, colour = Z)) +
opts(legend.position = "none")
HTH