I am trying to change the colors in a plot manually but
My code with dummydata:
df2=data.frame(y=runif(10), ontopic=c(F,F,F,F,F,T,T,T,T,T))
plot_right <- ggplot(df2, aes(y, fill = ontopic)) + geom_density(alpha = 0.5) +
coord_flip() + theme(legend.position = "none")+
scale_y_continuous(breaks=seq(0, 1, 0.5))+
scale_color_manual(values=c("#999999", "#E69F00"))
plot_right
This returns the standard colors. What do I need to do to change the colors to my manual selection?
Using scale_fill_manual instead of scale_colour_manual should work.
ggplot(df2, aes(y, fill = ontopic, colour= ontopic)) +
geom_density(alpha = 0.5) +
coord_flip() + theme(legend.position = "none")+
scale_y_continuous(breaks=seq(0, 1, 0.5)) +
scale_fill_manual(values=c("#999999", "#E69F00"))
Related
I am trying to change the colors in a plot manually but
My code with dummydata:
df2=data.frame(y=runif(10), ontopic=c(F,F,F,F,F,T,T,T,T,T))
plot_right <- ggplot(df2, aes(y, fill = ontopic)) + geom_density(alpha = 0.5) +
coord_flip() + theme(legend.position = "none")+
scale_y_continuous(breaks=seq(0, 1, 0.5))+
scale_color_manual(values=c("#999999", "#E69F00"))
plot_right
This returns the standard colors. What do I need to do to change the colors to my manual selection?
Using scale_fill_manual instead of scale_colour_manual should work.
ggplot(df2, aes(y, fill = ontopic, colour= ontopic)) +
geom_density(alpha = 0.5) +
coord_flip() + theme(legend.position = "none")+
scale_y_continuous(breaks=seq(0, 1, 0.5)) +
scale_fill_manual(values=c("#999999", "#E69F00"))
I'd like to ask how to delete legend in ggplot with regression model.
I already added theme(legend.position = "None")
but the legend cannot be deleted. Could you tell me what I was doing wrong?
Extra question!!
In my current code, how to change the symbol size and shape between N0 and N1? I want more bigger size of 'open circle', and 'closed square' shape.
Many thanks!!!
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen)) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
theme(legend.position = "None") +
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank())
This should work in lack of reproducible data. Be careful that functions like theme_bw() use to remove previous theme() settings as mentioned by #Ronald. So it is better to add in the final part of the plot. For shapes, you can enable shape in aes() like this and format with scale_shape_manual() (the numbers inside belong to the shape you want):
library(ggplot2)
#Code
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen,shape=Nitrogen,size=3)) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
scale_shape_manual(values = c(1,15))+
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank(),
legend.position = 'none')
For the legend: add the argument show.legend = F inside geom_point. For the different point size: can you give us an example of your dataset? We may need to reshape it.
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen), show.legend = F) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
theme(legend.position = "None") +
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank())
I have a graph like this. I am interested in the minor change between the range (-3.5, 0.5), however, the occupied only a small portion of the x-axis, so that it's hard to interpret.
I tried to use transform to log scale for better visualization, however, it apparently not works for negative values.
So is there any method to expand this region to make the graph look nicer?
Code:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18))
Something like this
library(ggforce)
library(ggolot2)
ggplot(mtcars, aes(x=mpg, y=disp, group=1)) +
geom_line() + facet_zoom(xlim = c(15, 20))
You may try adding the xlim = c(minor value, major value) option of ggplot, and use the range which works better for you
Something like that:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18)) +
xlim = c(-4, 1)
I am not able to increase the font size of the names of the variables in a graphic realized with ggplot.
I tried to include these codes inside ggplot code, but unsuccessfully :
theme(text = element_text(size=20))
theme(axis.text=element_text(size=20))
theme(axis.title=element_text(size=14))
theme_grey(base_size = 20)
geom_text(size=20)
My code is :
library(ggplot2)
library(reshape2)
dataplot <- read.csv("/Documents/R.csv",header=T,sep=";")
dataPlotMelt <- melt(data = dataplot, id.vars = c("variable"),variable.name = "Method",value.name = "SMD")
varNames <- as.character(dataplot$variable)
dataPlotMelt$variable <- factor(dataPlotMelt$variable,levels = varNames)
ggplot(data=dataPlotMelt,mapping=aes(x=variable,y=SMD,group=Method, color=Method))+
ylab("Standardizedmeandifference(%)")+
xlab("") +
geom_point(aes(shape=Method),size=2) +
geom_hline(yintercept=15,color="black",size=0.1,linetype="dashed") +
geom_hline(yintercept=-15,color="black",size=0.1,linetype="dashed") +
coord_flip() +
theme(axis.text.x=element_blank()) +
scale_y_continuous(breaks=c(-65,-15,15,105)) +
theme_bw() +
theme(legend.text=element_text(size=12)) +
theme(legend.title=element_blank(),legend.key=element_blank()) +
scale_colour_manual(values=c("grey","black"))
I'd like to increase the font size of the names of the variables in the graphic and, besides, increase the text "Standardized mean difference (%)" and remove the vertical line between the yintercept and ybreak on both sides
new graphic
Thank you Richard for giving me the solution.
As you suggested I used theme after theme_bw
I managed to suppress the useless vertical lines as well with the command theme(panel.grid.minor = element_blank())
Here is the new code for ggplot :
ggplot(data = dataPlotMelt, mapping = aes(x = variable, y = SMD,group = Method,
color = Method)) +
ylab("Standardized mean difference (%)") + xlab("") +
geom_point(aes(shape = Method),size=2) +
geom_hline(yintercept = 15, color = "black", size = 0.1, linetype = "dashed") +
geom_hline(yintercept = -15, color = "black", size = 0.1, linetype = "dashed") +
coord_flip() +
theme(axis.text.x = element_blank()) +
scale_y_continuous(breaks=c(-65,-15,0,15,105)) +
theme_bw() + theme(legend.text = element_text(size=13)) +
scale_colour_manual(values= c("grey","black")) +
theme(axis.text.y = element_text(size=12)) +
theme(axis.title.x = element_text(size=13)) +
theme(panel.grid.minor = element_blank()) +
theme(legend.title = element_blank(), legend.key=element_blank())
Maybe this is a very basic question but I'm a ggplot and R beginner.
I'm using this command to get a barplot:
ggplot(data=melt, aes(x=variable, y=value, fill=value)) +
geom_bar(width=.8, stat="identity") +
xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") +
theme(plot.title=element_text(face="bold", size=12)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) +
theme(axis.text.y = element_text(size=10))
I want to change the colors of barplot, but keeping the gradient of the colors depending on value column. I've tried this but I lose the gradient:
ggplot(data=melt, aes(x=variable, y=value, fill=value)) +
geom_bar(width=.8, stat="identity", fill="red") +
xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") +
theme(plot.title=element_text(face="bold", size=12)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) +
theme(axis.text.y = element_text(size=10))
The data is simple, only two columns( variable - value ):
variable value
1 nu73 13576.49
2 nu73t 10891.88
3 nu81 12673.33
4 nu81t 12159.91
5 nu83 12570.82
6 nu83t 11828.04
Thank you guys in advance
You want to adjust the scale, in particular the continuous scale of fill colors, hence the function scale_fill_continuous().
ggplot(data = melt, aes(x = variable, y = value, fill = value)) +
geom_bar(width = .8, stat = "identity") +
labs(x = "Samples", y = "Expression", title = "Gapdh") +
theme(plot.title = element_text(face = "bold", size = 12),
axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
axis.text.y = element_text(size = 10)) +
scale_fill_continuous(low = "firebrick4", high = "firebrick1")
(I slightly modified your plotting code: you can call theme once with multiple arguments, and I find labs nicer than a bunch of individual labeling calls.)
One other option is to use the palettes from the RColorBrewer package (which are incorporated into ggplot2). The scale_fill_brewer() scale if for discrete color scales, you can "distill" them into continuous scales with scale_fill_distiller(). For example
scale_fill_distiller(type = "seq", palette = "Reds")
To see all of the available scales, run RColorBrewer::display.brewer.all().