I would like to change the line size with a continuous variable. Now I used the geom_line with aesthetics size. For example:
x <- 1:100
y <- x * x
z <- abs(cos(x * pi / (max(x))))
df <- data.frame(x = x, y = y, z = z)
library(ggplot2)
ggplot(df, aes(x, y, size = z)) + geom_line()
But there are some spaces among segments (see figure below. Please zoom in to see the spaces). it seems ggplot2 uses rectangles to plot each segment.
I have increased the point number, but spaces till exist for bigger curvature.
My question is how to remove these spaces. I really appreciate it for any suggestions.
Adjust the multiplier to your preference:
mult <- 200
ggplot(df, aes(x, y)) + geom_line() + geom_ribbon(aes(ymin=y-mult*z, ymax=y+mult*z))
Related
I have a 4x4 geom_tile plot where the upper right cell contains an additional 4x4 cell. I am wanting the plot to appear as it does below.
The positions are listed inside the cells for clarity, however, the code makes the data value corresponding with the position placed in the cell. The sample code I provided currently produces this:
I am not able to manipulate the data values in terms of position (values for x and y cannot be changed) and the cell containing additional breakdown will not always be in the upper right so a solution that does not hardcode this location in would be appreciated.
If this is possible I would greatly appreciate any assistance you can provide!
x <- c(0,0,1,0.5,0.5,1,1)
y <- c(0,1,0,0.5,1,0.5,1)
data_val <- sample(0:100, 7)
alldata <-data.frame(x, y, data_val)
ggplot(data= alldata, aes(x, y)) +
geom_tile(colour = "black") +
geom_text(aes(label = data_val),colour="white")
This requires a slight change to your notation, so that x and y correctly locate the centre of each square, and an additional width parameter (which with this system really only depends on whether x and y are even or odd). For other quartered squares, use -1/+1 instead of 3/5 as appropriate.
x <- c(0,0,4,3,3,5,5)
y <- c(0,4,0,3,5,3,5)
w <- ifelse(x%%2==0, 4, 2)
data_val <- sample(0:100, 7)
alldata <- data.frame(x, y, w, data_val)
ggplot(data= alldata, aes(x=x, y=y, width=w, height=w)) +
geom_tile(fill = "white", color="black") +
geom_text(aes(label = data_val), colour="black") +
coord_fixed()
In R ggplot2, when I plot all zeros and use geom_jitter(), some variations are automatically added to zeros. How can I undo that? I still want all points at 0 y axis.
y = rep(0,100)
x = rep(c("A","B","C","D"),25)
D = data.frame(x,y)
library(ggplot2)
ggplot(D,aes(x=x,y=y))+geom_boxplot() + geom_jitter()
If you need to keep the dots and spread them horizontally, you can use geom_jitter(height = 0). This will force the vertical variation/jitter to zero, but still allows the points to "jitter" horizontally.
ggplot(D, aes(x = x, y = y)) +
geom_boxplot() +
geom_jitter(height = 0)
I am trying to create a plot where the color gradient changes by both the x and y axis. More specifically I am trying set up the gradients so that the hue range changes along the x axis and the value changes along the y axis.
For an example I am working with a sine curve with some noise along -pi to pi.
set.seed(5678)
x <- seq(-1*pi, 1*pi, 0.01)
y <- sin(x) + rnorm(length(y))
df <- cbind.data.frame(x, y)
ggplot(df, aes(x=x, y=y)) + geom_line()
Now I want to colorize the line so that the hue progresses from red-orange to orange-yellow to yellow-green, etc. along the x axis and then will take on different values in that range depending on its y value. So at x=-pi, y=2 might be red and y=-2 might be yellow while at x=0, y=2 might be green and y=-2 might be blue.
Has anyone tried to create a graph like this?
Here's an option for doing it using a hue calculated from x and y:
df$hue <- pmax(pmin((df$x + pi)/pi/3 + (2 - df$y) / 12, 1), 0)
ggplot(df, aes(x=x, y=y, group = 1, colour = hsv(hue, 1, 1))) + geom_path() +
scale_colour_identity()
Note because the lines are quite long vertically so the effect isn't fully seen. Here's a version using approx to interpolate:
adf <- as.data.frame(approx(df, xout = seq(-pi, max(df$x), 0.001)))
adf$hue <- pmax(pmin((adf$x + pi)/pi/3 + (2 - adf$y) / 12, 1), 0)
ggplot(adf, aes(x=x, y=y, group = 1, colour = hsv(hue, 1, 1))) + geom_path() +
scale_colour_identity()
In both cases, it's the hue that's dependent on both x and y, with value held constant. That fits your proposed example, if not your original description. Clearly it could be tailored to vary hue and value separately. It's also worth noting that there needs to be a group set. Otherwise ggplot2 tries to join together all the points of the same colour.
A great answer on how to label the count on geom_bin2d, can be found here:
Getting counts on bins in a heat map using R
However, when modifying this to have a logarithmic X axis:
library(ggplot2)
set.seed(1)
dat <- data.frame(x = rnorm(1000), y = rnorm(1000))
# plot MODIFIED HERE TO BECOME log10
p <- ggplot(dat, aes(x = x, y = y)) + geom_bin2d() + scale_x_log10()
# Get data - this includes counts and x,y coordinates
newdat <- ggplot_build(p)$data[[1]]
# add in text labels
p + geom_text(data=newdat, aes((xmin + xmax)/2, (ymin + ymax)/2,
label=count), col="white")
This produces labels that are very poorly mapped to their respective points.
How can I correct the geom_text based labels to correctly map to thier respective points?
Apply logarithmic transformation directly on x values, not on scale. Change only one line of your code:
p <- ggplot(dat, aes(x = log10(x), y = y)) + geom_bin2d()
That allows to keep negative values and produces the following plot:
Like this previous poster, I am also using geom_text to annotate plots in gglot2. And I want to position those annotations in relative coordinates (proportion of facet H & W) rather than data coordinates. Easy enough for most plots, but in my case I'm dealing with histograms. I'm sure the relevant information as to the y scale must be lurking in the plot object somewhere (after adding geom_histogram), but I don't see where.
My question: How do I read maximum bar height from a faceted ggplot2 object containing geom_histogram? Can anyone help?
Try this:
library(plyr)
library(scales)
p <- ggplot(mtcars, aes(mpg)) + geom_histogram(aes(y = ..density..)) + facet_wrap(~am)
r <- print(p)
# in data coordinate
(dc <- dlply(r$data[[1]], .(PANEL), function(x) max(x$density)))
(mx <- dlply(r$data[[1]], .(PANEL), function(x) x[which.max(x$density), ]$x))
# add annotation (see figure below)
p + geom_text(aes(x, y, label = text),
data = data.frame(x = unlist(mx), y = unlist(dc), text = LETTERS[1:2], am = 0:1),
colour = "red", vjust = 0)
# scale range
(yr <- llply(r$panel$ranges, "[[", "y.range"))
# in relative coordinates
(rc <- mapply(function(d, y) rescale(d, from = y), dc, yr))