I am trying to make a graph in ggplot2. I want the x-axis to show 2.84 along with the sequence typed below. Is there any other way beside typing all the exact values in breaks()? I tried google but it doesn't solve my problem.
scale_x_continuous(limits = c(1, 7), seq(1,7,by=0.5), name = "Number of
treatments")
You can programmatically generate specific breaks, like this:
# make up some data
d <- data.frame(x = 6*runif(10) + 1,
y = runif(10))
# generate break positions
breaks = c(seq(1, 7, by=0.5), 2.84)
# and labels
labels = as.character(breaks)
# plot
ggplot(d, aes(x, y)) + geom_point() + theme_minimal() +
scale_x_continuous(limits = c(1, 7), breaks = breaks, labels = labels,
name = "Number of treatments")
Related
I am trying to add a caption in each facet (I am using facet_grid). I have seen these approach and this one: but nothing gives me what I need. Also, the first approach returns a warning message that I didn't find any solution:
Warning message:
Vectorized input to `element_text()` is not officially supported.
Results may be unexpected or may change in future versions of ggplot2.
My example:
library(ggplot2)
library(datasets)
mydf <- CO2
a <- ggplot(data = mydf, aes(x = conc)) + geom_histogram(bins = 15, alpha = 0.75) +
labs(y = "Frequency") + facet_grid(Type ~ Treatment)
a
caption_df <- data.frame(
cyl = c(4,6),
txt = c("1st=4", "2nd=6")
)
a + coord_cartesian(clip="off", ylim=c(0, 3)) +
geom_text(
data=caption_df, y=1, x=100,
mapping=aes(label=txt), hjust=0,
fontface="italic", color="red"
) +
theme(plot.margin = margin(b=25))
The idea is to have 1 caption per plot, but with this approach it repeats the caption and it is overwritten.
Is it possible to have something like this? (caption OUTSIDE the plot) (but without the previous warning)
a + labs(caption = c("nonchilled=4", "chilled=6")) + theme(plot.caption = element_text(hjust=c(0, 1)))
NOTE: This is only an example, but I may need to put long captions (sentences) for each plot.
Example:
a + labs(caption = c("This is my first caption that maybe it will be large. Color red, n= 123", "This is my second caption that maybe it will be large. Color blue, n= 22")) +
theme(plot.caption = element_text(hjust=c(1, 0)))
Does anyone know how to do it?
Thanks in advance
You need to add the same faceting variable to your additional caption data frame as are present in your main data frame to specify the facets in which each should be placed. If you want some facets unlabelled, simply have an empty string.
caption_df <- data.frame(
cyl = c(4, 6, 8, 10),
conc = c(0, 1000, 0, 1000),
Freq = -1,
txt = c("1st=4", "2nd=6", '', ''),
Type = rep(c('Quebec', 'Mississippi'), each = 2),
Treatment = rep(c('chilled', 'nonchilled'), 2)
)
a + coord_cartesian(clip="off", ylim=c(0, 3), xlim = c(0, 1000)) +
geom_text(data = caption_df, aes(y = Freq, label = txt)) +
theme(plot.margin = margin(b=25))
I'm trying to use ggplot to create a bar plot (or histogram) to mirror the freq function from the descr package (where each discrete value in the variable gets its own column in the frequency plot, with the x-ais ticks centered around each value), but I'm having some trouble getting this to work.
Here is what I'm trying to create (but using ggplot so I can use its nice graphics):
library(ggplot2)
library(descr)
variable <- c(0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 7)
df <- data.frame(variable)
freq(df$variable)
And here is my trying (and failing) to do the same in ggplot:
histo.variable <- ggplot(df, aes(x = variable)) + # create histogram
geom_bar(stat = "bin") +
xlab("Variable Value") +
ylab("Count") +
scale_x_continuous(breaks = scales::pretty_breaks(n = 10))
histo.variable
As you can see, the bars are not centered on the tick marks. (Additionally, it'd be great to get rid of the little half-lines in between the bars.)
Thanks to anyone who can help!
Maybe like this:
ggplot(df, aes(x = variable)) +
geom_histogram(aes(y = ..density..),
binwidth = 1,
colour = "blue", fill = "lightblue")
I have two dataframes that give temperatures for two different times for the same set of coordinates. I'd like to plot the two together using the same color for the same temperature between the two plots; that is, I'd like to have one legend for the two plots that takes into account the fact that the range of temperatures for the two plots is not the same.
I thought that I might be able to use scale_colour_manual(), but I'm not sure how to implement it in this case where I essentially have continuous variables (my real data have tens of thousands of observations). Here is an example of what I have so far:
# Create sample data
dat1 <- data.frame(c(-158.28, -158.27, -158.26),
c(21.57, 21.56, 21.57), c(24, 22, 25))
names(dat1) <- c('x', 'y', 'Temp.')
dat2 <- data.frame(c(-158.28, -158.27, -158.26),
c(21.57, 21.56, 21.57), c(22, 20, 23))
names(dat2) <- c('x', 'y', 'Temp.')
# Create plots
plot1 <- ggplot(dat1, aes(x,y)) + geom_point(aes(color = Temp.)) +
scale_colour_gradientn(colours = rev(rainbow(4))) +
theme(
title = element_text('January 1, 1990 at 00:00'),
legend.position = 'none') +
ggtitle('Jan. 1, 1990 00:00')
plot2 <- ggplot(dat2, aes(x,y)) + geom_point(aes(color = Temp.)) +
scale_colour_gradientn(colours = rev(rainbow(4))) + theme(
title = element_text('August 18, 2007 at 02:00'),
legend.position = 'none') +
ggtitle('Aug. 18, 2007 14:00')
# Combine plots
grid.arrange(plot1, plot2, ncol = 2, nrow = 1, respect = T)
I'd like to change the color scale so that it is relative to the combined temperatures of the two figures, not the individual figures. This code won't work, but it shows what I'm looking to do:
scale_colour_gradientn(colours = rev(rainbow(4,
start = min(c(dat1$Temp.,dat2$Temp.)),
end = max(c(dat1$Temp.,dat2$Temp.)))))
In ggplot2, things like this get a lot easier when you make a single plot with multiple facets - try this:
# Add identifiers to each set
dat1$dtime <- 'Jan. 1, 1990 00:00'
dat2$dtime <- 'Aug. 18, 2007 14:00'
# Stack them
dat_all <- rbind(dat1, dat2)
# Plot all at once
ggplot(dat_all, aes(x = x, y = y, color = `Temp.`)) +
geom_point() +
facet_wrap( ~ dtime) +
scale_colour_gradientn(colours = rev(rainbow(4)))
I have some time series data and I would like to customize the x-axis (dates) to show the date labels where I obtain measurements, as opposed to having regular breaks per week/month/year.
Sample data:
dates <- as.Date("2011/01/01") + sample(0:365, 5, replace=F)
number <- sample(1:100, 5)
df <- data.frame(
dates = dates,
number = number
)
This way I can plot my df with regular breaks every month...
ggplot(df, aes(as.Date(dates), number)) +
geom_point(size=6) +
geom_segment(aes(x = dates, y = 0, xend = dates, yend = number),
size=0.5, linetype=2) +
scale_x_date(breaks = date_breaks("1 month"), labels = date_format("%d-%b-%Y")) +
theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5))
... but I would like to set the major breaks to the actual 5 dates in df$dates. It works with a normal continuous scale (scale_x_continuous(breaks = c(1, 3, 7, 9))) but I can't figure out how to do it for a continuous date scale.
I am looking to do something like...
scale_dates_continuous(breaks = df$dates)
...but that doesn't exist unfortunately. Thanks lot for your help!
Please read ?scale_x_date, about the breaks argument: you can use a "vector of breaks". Thus, try
scale_x_date(breaks = df$dates, labels = date_format("%d-%b-%Y"))
Let us say I have the following graph plotted using ggplot:
Is there anyway to extend how much line length is displayed in the legend? Sometimes, it just gets impossible to identify which line correspond to which line in the graph using the legend.
here is an option legend.key.width:
# sample data frame
df <- data.frame(x = c(rnorm(100, -3), rnorm(100), rnorm(100, 3)),
g = gl(3, 100))
df <- ddply(df, .(g), summarize, x = x, y = ecdf(x)(x))
ggplot(df, aes(x, y, colour = g, linetype = g)) +
geom_line() +
theme(legend.key.width = unit(10, "line"))
opts is not working with ggplot2. You need to use theme, so instead you need to type:
+ theme(legend.key.width = unit(10, "line"))