Hex colour not plotting correctly in ggplot2 (R) [duplicate] - r

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

How to delete legend in ggplot with reression model?

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())

Set variable colors on ggplot2 [duplicate]

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"))

Scale the distance between ticks on x-axis in gglot r

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)

In ggplot2 how can I scale the legend when using two graph types?

I'm using ggplot2 with both + geom_line() + geom_point(). I have the colors/shapes worked out, but I can't scale the legend appropriately. If I do nothing it's tiny, and if I enlarge it, the color blocks the shape.
For example:
You can see that the shapes and colors are both in the legend, but the shapes are being drawn over by the colors. I would like to have shapes of the appropriate color drawn in the legend, but can't figure out how to do it.
My plot is being drown as follows:
ggplot(data=melted, aes(x=gene, y=value, colour=variable, shape=variable, group = variable, stroke=3, reorder(gene, value)))
+ theme_solarized()
+ scale_colour_solarized("blue")
+ geom_line()
+ geom_point()
+ theme(axis.text.x = element_text(angle = 90, hjust = 1), plot.title = element_text(size=16, face="bold"), legend.title=element_blank(), legend.text=element_text(size=20))
+ ggtitle('Signiture Profiles')
+ labs(x="Gene", y=expression(paste("Expression"), title="Expression"))
+ scale_colour_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c("#ff420e","#89da59","#89da59","#89da59","#376467","#376467","#376467","#00293c","#00293c","#00293c"))
+ scale_shape_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c(0,1,2,3,1,2,3,1,2,3))
+ guides(colour = guide_legend(override.aes = list(size=12)))
Here is some example data as requested:Example Data
Thanks in advance for any help you can provide.
You could perhaps rethink how you are differentiating your variables.
You could do something like the following. Note the changes in the first line, where I have separated the component parts of variable rather than setting colours and shapes via your scale statements. (I haven't got your theme, so I left that out).
ggplot(data=melted, aes(x=gene,
y=value,
colour=gsub("_.*","",variable),
shape=gsub(".*_","",variable),
group = variable,
stroke=3,
reorder(gene, value))) +
geom_line() +
geom_point() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
plot.title = element_text(size=16, face="bold"),
legend.title=element_blank(),
legend.text=element_text(size=20)) +
ggtitle('Signiture Profiles') +
labs(x="Gene", y=expression(paste("Expression"), title="Expression")) +
guides(shape = guide_legend(override.aes = list(size=5)),
colour = guide_legend(override.aes = list(size=5)))

ggplot change fill colour without losing colour gradient

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().

Resources