Using ggplot2 geom_tile, the default location of the tiles is centred on values of x and y. Is there a way to get x and y values to be the bottom left corner of each tile.
From http://docs.ggplot2.org/current/geom_tile.html
x.cell.boundary <- c(0, 4, 6, 8, 10, 14)
example <- data.frame(
x = rep(c(2, 5, 7, 9, 12), 2),
y = factor(rep(c(1,2), each=5)),
z = rep(1:5, each=2),
w = rep(diff(x.cell.boundary), 2)
)
qplot(x, y, fill=z, data=example, geom="tile")
I do not like my answer, but I'll post it anyway while waiting for a better solution. I transform the data (x-axis +1 and y-axis +0.5) and use the real data as axis breaks.
example <- data.frame( x = rep(c(3, 6, 8, 10, 13), 2), y = (rep(c(1.5,2.5), each=5)), z = rep(1:5, each=2))
ggplot(example)+ geom_tile(aes(x,y,fill=z)) +
scale_x_continuous(breaks=c(2, 5, 7, 9, 12))+
scale_y_continuous(breaks=c(1,2))
Related
I have a faceted graph with a strip label which is clipped due to the width of the facets - I have been removing this clip manually in Inkscape but would like to do it in R. See this small reproducible example (the figure width is non-negotiable and needs to be exported as a .eps file):
library(tidyverse)
# Build data frame
df <- data.frame(treatment = factor(c(rep("A small label", 5), rep("A slightly too long label", 5))),
var1 = c(1, 4, 5, 7, 2, 8, 9, 1, 4, 7),
var2 = c(2, 8, 11, 13, 4, 10, 11, 2, 6, 10))
# Plot scatter graph with faceting by 'treatment'
p <- ggplot(df, aes(x = var1, y = var2)) +
geom_point() +
facet_wrap(treatment ~ ., ncol = 2)
# Save graph as .eps
ggsave(filename = "Graph1.eps", plot = p, device = "eps", width = 60, height = 60, units = "mm")
What I would like is this, where the facet label extends beyond the width of the facet:
So far I've tried the following from this StackOverflow question:
# This doesn't affect the strip labels
p2 <- p +
coord_cartesian(clip = "off")
ggsave(filename = "Graph.eps", plot = p2, device = "eps", width = 60, height = 60, units = "mm")
# This doesn't affect strip labels and results in a blank white graph when exported using ggsave
p3 <- p
p3$layout$clip = "off"
ggsave(filename = "Graph.eps", plot = p3, device = "eps", width = 60, height = 60, units = "mm")
I also tried this way of turning the layout$clip off from this question but it has the same issues as above with the strip labels still being clipped and ggsave exporting a blank file.
p4 <- ggplot_gtable(ggplot_build(p))
p4$layout$clip[p4$layout$name == "panel"] <- "off"
p4 <- grid.draw(p4)
ggsave(filename = "Graph.eps", plot = p4, device = "eps", width = 60, height = 60, units = "mm")
EDIT: As of ggplot2 3.4.0, this has been integrated.
There is a feature request with an open PR on the ggplot2 github to make strip clipping optional (disclaimer: I filed the issue and opened the PR). Hopefully, the ggplot2 team will approve it for their next version.
In the meantime you could download the PR from github and try it out.
library(ggplot2) # remotes::install_github("tidyverse/ggplot2#4223")
df <- data.frame(treatment = factor(c(rep("A small label", 5), rep("A slightly too long label", 5))),
var1 = c(1, 4, 5, 7, 2, 8, 9, 1, 4, 7),
var2 = c(2, 8, 11, 13, 4, 10, 11, 2, 6, 10))
# Plot scatter graph with faceting by 'treatment'
p <- ggplot(df, aes(x = var1, y = var2)) +
geom_point() +
facet_wrap(treatment ~ ., ncol = 2) +
theme(strip.clip = "off")
ggsave(filename = "Graph1.eps", plot = p, device = "eps", width = 60, height = 60, units = "mm")
I am working with the R programming language. Normally when I make plots, I am using the ggplot2 library and the aes() options can be used to label the x-axis and add a title. However this time, I the plots I am making are not ggplot2 objects, and therefore can not be labelled in the same way:
library(MASS)
library(plotly)
a = rnorm(100, 10, 10)
b = rnorm(100, 10, 5)
c = rnorm(100, 5, 10)
d = matrix(a, b, c)
parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)
#error - this is also apparent because the ggplotly() command can not be used.
ggplotly(d)
Does anyone know how to add labels on the x-axis of this plot and some title? Can the ggplotly command be used here?
Thanks
You can use title(), e.g.
library(MASS)
a = rnorm(100, 10, 10)
b = rnorm(100, 10, 5)
c = rnorm(100, 5, 10)
d = matrix(a, b, c)
parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)
title(main = "Plot", xlab = "Variable", ylab = "Values")
axis(side = 2, at = seq(0, 5, 0.1),
tick = TRUE, las = 1)
I would like to plot a stacked bar plot in R and my data looks as such:
This table is the values against date and as it can be seen, there are repetitive dates with different sides. I would like to plot a bar plot using this data.
combined = rbind(x,y)
combined = combined[order(combined$Group.1),]
barplot(combined$x,main=paste("x vs y Breakdown",Sys.time()),names.arg = combined$Group.1,horiz = TRUE,las=2,xlim=c(-30,30),col = 'blue',beside = True)
Want a stacked plot where I can see the values against dates. How do change my code?
You can easily create this figure with ggplot2.
Here a piece of code for you using a data frame similar to what you have:
library(ggplot2)
my_data <- data.frame(
date = factor(c(1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8)),
x = c(-2, 14, -8, -13, 3, -4, 9, 8, 3, -4, 8, -1)
)
g <- ggplot(my_data, aes(x = date, y = x)) +
geom_bar(
stat = "identity", position = position_stack(),
color = "white", fill = "lightblue"
) +
coord_flip()
This is the output:
Obviously, the official documentation is a good way to start to understand a bit better how to improve it.
I have 2 data sets (DSA and DSB) that contain x & y coordinates
tumor<- data.frame(DSA[,c("X_Parameter","Y_Parameter")])
cells<-data.frame(DSB[,c ("X_Parameter","Y_Parameter")])
plot(cells, xlim=c(1,1300), ylim=c(1,1000), col="red")
par(new=TRUE)
plot(tumor, xlim=c(1,1300), ylim=c(1,1000), col="blue")
the plots make this graph
I want to be able to draw a connecting line from every red dot to every blue dot.
Does anyone know if this can be done. thanks
Sample
DSA=(5,5 6,6 5,6 6,5) DSB=(1,1 10,10 10,1 1,10)
what the plot should look like
Brute-force, perhaps inelegant:
DSA <- data.frame(x = c(5, 6, 5, 6),
y = c(5, 6, 6, 5))
DSB <- data.frame(x = c(1, 10, 10, 1),
y = c(1, 10, 1, 10))
plot(y ~ x, DSB, col = "red")
points(DSA, col = "blue")
for (r in seq_len(nrow(DSA))) {
segments(DSA$x[r], DSA$y[r], DSB$x, DSB$y)
}
Edit: more directly:
nA <- nrow(DSA)
nB <- nrow(DSB)
plot(y ~ x, DSB, col = "red")
points(DSA, col = "blue")
segments(rep(DSA$x, each = nB), rep(DSA$y, each = nB),
rep(DSB$x, times = nA), rep(DSB$y, times = nA))
(I still can't figure out an elegant solution with #42's recommendation for combn or outer.)
stripchart :
x <- c(2, 8, 11, 19)
stripchart(x)
How do you add labels 2, 8, 11, 19 next to the points?
Use text and specify the y position. The stripchart is drawn with y=1, so text(x, y=1.1, ...) will draw the labels slightly above the points.
x <- c(2, 8, 11, 19)
stripchart(x)
text(x, 1.1, labels=x)