I have the following code to plot a histogram using ggplot2.
ggplot(data = plot, aes(x = total_Amg))+
geom_histogram(binwidth = 1, colour = "black", fill = "white") +
facet_wrap(~ gentamicin)
How can I specify what numbers are shown on the x axis of the resulting graph, i.e. show 0 to 8, rather than showing those that was automatically generated (see below)?
+scale_x_continuous(breaks=0:8) should do the trick
Try adding:
+ scale_x_continuous(breaks = 0:8, labels = 0:8)
Related
I'm trying to use ggplot2 to make some sort of timeline using values from a dataframe (df). I've managed to plot the data exactly how I want it (the different colored line segments connecting the x-marks in this exact order, i.e., from left to right: 'early', 'unknown', 'late', 'sub'). The startpoint and endpoint columns in the dataframe are used to define the positions of the points and line segments.
The problem is that the legend doesn't show the color of the 'x' icons, they are just grey. I've tried adding scale_color_manual() and scale_fill_manual() commands but they don't seem to change anything. The legend does display the correct color when I change the shape to shape = 21, however, I really want the shape to be 4 (x icons). I don't care about the shape of the legend though but scale_shape_manual() again didn't change anything about the legend.
I have also tried placing different color arguments inside and outside the aes() argument of ggplot(), geom_segment() and/or geom_point().
How can I make the icons from the legend show the correct color?
Below I added a piece of code to reproduce the problem.
library(ggplot2)
library(RColorBrewer)
## Define dataframe
df <- data.frame(Var = c("sub","late","unknown","early"),
Time = c(10,267,0,1256),
Endpoint = c(1533,1523,1256,1256),
Startpoint = c(1523,1256,1256,0))
colorscheme <- RColorBrewer::brewer.pal(9, "Set1")[c(1,4,2,3)]
## Make plot
ggplot(df, aes(x="", y=Endpoint, fill=Var), color =colorscheme) +
geom_segment( aes(x="", xend="", y=Startpoint, yend=Endpoint), color = colorscheme) +
geom_point(aes(x="", y=Endpoint),size=5, shape=4 , color = colorscheme) +
coord_flip()
Thanks in advance for any suggestions!
You should use color instead of fill. To remove the line from the legend, use guides(color = guide_legend(override.aes = list(linetype = 0))) or use show.legend = F in geom_segment.
Also, arguments passed in ggplot need not to be repeated afterward.
ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme) +
geom_segment(aes(xend="", y=Startpoint, yend=Endpoint)) +
geom_point(size=5, shape=4) +
coord_flip() +
guides(color = guide_legend(override.aes = list(linetype = 0)))
#or
ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme) +
geom_segment(aes(xend="", y=Startpoint, yend=Endpoint)) +
geom_point(size=5, shape=4) +
coord_flip()
Try this:
ggplot(df, aes(x = "", y = Endpoint, color = Var), colorscheme) +
geom_segment(aes(x = "", xend = "", y = Startpoint, yend = Endpoint), show.legend = FALSE) +
geom_point(aes(x = "", y = Endpoint), size = 5, shape = 4) +
coord_flip()
In this way legend will show only X
Hi guys, I have this very messy plot. How can I
rotate the x axis text so that you can actually read it
not include every y value in the y-axis (maybe have the y axis in intervals of 5)
add jitter so that the plot is easier is read
remove the NA values (I tried to, but I guess it did not work)
remove the legend (had to crop it for confidentiality)
here is my code:
data <- ndpdata[which(ndpdata$FC.Fill.Size==20),] #20 fill size
library(tidyr)
my_df_long <- gather(data, group, y, -FC.Batch.Nbr)
data = my_df_long[2075:2550,]
ggplot(data, aes(FC.Batch.Nbr, y, color=FC.Batch.Nbr), na.rm=TRUE) + geom_point()
To rotate the x axis add this to your ggplot:
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
If you don't want to include every value on the y axis, you can set breaks:
scale_y_continuous(breaks = c(251,270,290,310,325))
To add jitter points try position = "jitter" inside geom_point():
geom_point(position = "jitter")
To remove NA's you can use it on your data:
data <- data[!is.na(data)]
To remove legend add this to your ggplot:
theme(legend.position = "none")
So I have a dataset of performance scores with an associated difficulty value, and I want to display the average performance score per difficulty value. The difficulty values range from 0 to 10, but have up to 10 decimal points and as a result are hyper specific. To make this more legible, I've been grouping the difficulty scores into bins. I've done this at two different resolutions, bins of width 0.1, and bins of width 1.
What I would like to do, is display a line plot (using the finer data points), on top of a bar plot (using the wider resolution), but I want the bar plot to maintain its structure. Right now, when I try to overlay the line plot, the x-axis seems to scale to the line plot, and the bars end up extremely narrow.
Here's the bar plot code:
g1.4 = ggplot() +
geom_bar(data = grouped_diff_wide, aes(y=mean_diff_perf, x=gr, fill=subject), stat = "identity" )+
facet_wrap(~subject)+
ggtitle("Average Performance By Difficulty")+
labs(fill = "Subject")+
ylab("Performance")+
xlab("Difficulty")+
scale_x_discrete(breaks = diff_breaks_wide, labels = seq(0, 9, 1))
g1.4
And the resulting graph:
just the bar plot
Here's the line plot code:
g1.5 = ggplot() +
geom_line(data = grouped_diff_fine, aes(y=mean_diff_perf, x = gr, group = 1))+
facet_wrap(~subject)+
ggtitle("Average Performance By Difficulty")+
labs(fill = "Subject")+
ylab("Performance")+
xlab("Difficulty")+
scale_x_discrete(breaks = diff_breaks_fine, labels = seq(0, 9, 1))
g1.5
And the resulting graph: just the line plot
And here's my attempt to combine them:
g1.6 = ggplot() +
geom_bar(data = grouped_diff_wide, aes(y=mean_diff_perf, x=gr, fill=subject), stat = "identity" )+
geom_line(data = grouped_diff_fine, aes(y=mean_diff_perf, x = gr, group = 1))+
facet_wrap(~subject)+
ggtitle("Average Performance By Difficulty")+
labs(fill = "Subject")+
ylab("Performance")+
xlab("Difficulty")+
scale_x_discrete(breaks = diff_breaks_fine, labels = seq(0, 9, 1))
g1.6
And how it turns out: combined plot with skinny bars
Is there a way to maintain the proportions of the stand alone bar plot but with the line plot overlayed?
you can use the width parameter of geom_bar (reference see here). As a very simple example using the built-in mtcars data:
ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_bar(stat = "identity", width = 1.1) +
geom_line(colour = "blue", size = 2)
I would like to plot a density plot using ggplot2, and make one section of the x-axis line thicker (or colored differently).
For example:
interval <- c(x1,x2)
x <- ggplot(df, aes(x=value)) + geom_density()
Is there any way to selectively make the x-axis segment corresponding to (x1,x2) thicker or colored differently? Thanks.
You can use annotate to add a line segment. Setting the y coordinates to -Inf will place it on the x axis. Since your example isn't reproducible, I've demonstrated on the mtcars data:
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
annotate(
geom = "segment",
x = 3, xend = 4,
y = -Inf, yend = -Inf,
color = "blue",
size = 5
)
I am developing a graph in R with ggplot2 that has two geoms (one geom_line and one geom_text). It draws a line graph and then places text labels on start and end points of each line segment.
(myplot <- ggplot(data=datatable, aes(x, y, group = group,colour = group, label=mylabels)) + geom_line(size = 1.5))
myplot + geom_text(color = "black")
Now my question is how can I do the following tasks in ggplot2, they all work when I only have one geom but not with both (seems that they overide each other)
1 - making the background white.
The following code works with geom_line but as soon as I add geom_text it becomes gray again. Even if I add this line after geom_text it gets rid of the point labels that are on the chart.
myplot + opts(panel.background = theme_rect(fill = "white", colour = NA))
2- x labels and x label format. Again the following code works with only one geom but breaks when I have the second geom
myplot + scale_x_date(format="%m", 'my x label')
3- While we are on it how can I put the legend at the bottom and spread it horizontally (p + opts(legend.position="bottom")) spreads that vertically that looks very stupid.
For 1), you haven't saved the object myplot after the second and third calls involving it. This works for me:
set.seed(3)
dat <- data.frame(x = runif(10), y = rnorm(10), group = gl(2,5),
mylabel = paste(1:10, "foo"))
require(ggplot2)
myplot <- ggplot(data=dat, aes(x, y, group = group, colour = group,
label = mylabel)) + geom_line(size = 1.5)
myplot + geom_text(color = "black") +
opts(panel.background = theme_rect(fill = "white", colour = NA))
Note that I only ever save myplot once. The second call involving myplot modifies it on the fly but doesn't save it.
For the rest, you'll need to provide a reproducible example.