Making symbols bold in ggplot2 - r

I want to use pch=3 in ggplot2 geom_point and I want to make it bold. I can increase the size but could not make it bold. Any suggestions?
libray(ggplot2)
z=data.frame(x=1:12,y=c(3,5,1,6,2,9,7,10,11,4,12,8))
ggplot(z,aes(x=x,y=y))+geom_point(pch=3,size=5)

You can use stroke argument:
library(ggplot2)
z=data.frame(x=1:12,y=c(3,5,1,6,2,9,7,10,11,4,12,8))
ggplot(z,aes(x=x,y=y))+geom_point(pch=3,size=5, stroke = 2)

Related

Manual coloring of dots by set color in Complex Upset

I would like to create an UpSet plot using the ComplexUpset package. I would like to manually color the dots in the intersection matrix to match the sets' color.
Here's what I've attempted so far:
movies <- read.csv( system.file("extdata", "movies.csv", package = "UpSetR"), header=T, sep=";" )
ComplexUpset::upset(movies, colnames(movies)[3:5], name='genre', width_ratio=0.1,
matrix=(intersection_matrix(geom=geom_point()) +
scale_color_manual(values = c('Action'='red', 'Adventure'='blue', 'Children'='yellow'))),
queries=list(upset_query(set='Action', fill='red'),
upset_query(set='Adventure', fill='blue'),
upset_query(set='Children', fill='yellow')))
Adding scale_color_manual only resulted in an extra legend of dots that was plotted which I do not want. I would like the dots, in the intersection matrix itself, for 'Action' to be red, the dots for 'Adventure' to be blue and the dots for 'Children' to be yellow. The segment line connecting the dots should remain black.
The dots don't change color because you are passing fill (not color) to upset_query; you can keep using fill if you like but in that case you need to also change the shape of the dot so that they have area to be filled, which can be done using shape='circle filled' (there are other shapes too!).
Similarly, the scale_color_manual would not be a perfect choice in plain ggplot2, but given that it is overridden by ComplexUpset it is actually a good idea, but you will need to customize override.aes to make it display as intended.
library(ComplexUpset)
library(ggplot2)
upset(
movies,
colnames(movies)[3:5],
name='genre',
width_ratio=0.1,
matrix=(
intersection_matrix(geom=geom_point(shape='circle filled', size=3))
+ scale_color_manual(
values=c('Action'='red', 'Adventure'='blue', 'Children'='yellow'),
guide=guide_legend(override.aes=list(shape='circle'))
)
),
queries=list(
upset_query(set='Action', fill='red'),
upset_query(set='Adventure', fill='blue'),
upset_query(set='Children', fill='yellow')
)
)
Alternatively, you could simply pass color in addition to fill, but I think that the fill option give nicer visuals.

ggplot2, why so blue? How to set a different color palette in gradient

Why doesn't this pick up the green colors? It stays blue...
require(ggplot2)
data(iris)
ggplot(iris,aes(Sepal.Length,Sepal.Width,col=Petal.Length,pch=Species,size=Petal.Width))+
scale_fill_gradient(low="green",high="darkgreen")+
ggtitle('Why so blue?')+geom_point()
Any suggestions?
You've used scale_fill_gradient(), yet your aes() contains a color argument. Try scale_color_gradient() instead.
data(iris)
ggplot(iris,aes(Sepal.Length,Sepal.Width,
col=Petal.Length,pch=Species,size=Petal.Width))+
scale_color_gradient(low="green",high="darkgreen")+
ggtitle('Now so green')+geom_point()
Output:
Explanation:
Whether to use scale_fill or scale_color is determined by the arguments supplied to the aes() portion of ggplot code. Color arguments are typically used for things like geom_point() and geom_line(); fill arguments are typically used for the color of bars in a bar graph for example.

Legend: change of fill color R plot

I was wondering how I could change the fill color of the square next to beta in my legend below to be according to: adjustcolor("blue",alpha.f=0.05)?
Here is my current R code (with no success):
curve(dt(x,19),-5,5)
legend("topright",legend=c(expression(paste("Power")),expression(paste(beta))),
density=c(15,0),angle=140,fill=T,col=c(adjustcolor("blue",alpha.f=0.05),0))
Something like this should work:
legend("topright",legend=c(expression(paste("Power")),expression(paste(beta))),
density=c(15,0),angle=140,fill=T,border=c('black', 'blue'))
[EDITED]
legend("topright",legend=c(expression(paste("Power")),expression(paste(beta))),
density=c(15,NA),angle=140,fill=c("#000000FF", adjustcolor("blue",alpha.f=0.5)),
border=c("#000000FF", adjustcolor("blue",alpha.f=0.5)))

R: adding a plot legend in R

plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species)
I know that I can use the legend() function to manually set my legend. However, I have no idea which color was assigned to the different species in my data? Is there an automatic way to get plot() to add a legend?
As #rawr says, palette() determines the colour sequence used. If you use integers to specify colours, it will also look at palette(). Thus
with(iris,plot(Sepal.Length, Sepal.Width, col = Species))
legend("topright",legend=levels(iris$Species),col=1:3, pch=1)
works nicely.
Base R doesn't have an auto-legend facility: the ggplot2 package does.
library(ggplot2)
ggplot(iris,aes(Sepal.Length,Sepal.Width,colour=Species))+geom_point()
gives you a plot with an automatic legend (use theme_set(theme_bw()) if you don't like the grey background).
The built-in lattice package can also do automatic legends:
library(lattice)
xyplot(Sepal.Width~Sepal.Length,group=Species,data=iris,auto.key=TRUE)

Can I use direct.label() with ggplot's scale_colour_manual()?

The following code assigns a manual color scale of red and black to my points:
require(ggplot2)
require(directlabels)
dtest <- data.frame(x=1:20,
y=rnorm(20,0,5),
v=seq(1,2))
p <- ggplot(dtest, aes(x=x,y=y,color=as.factor(v))) + geom_point() + scale_colour_manual(values=c("red","black"))
p #this looks good; red and black as intended
direct.label(p) #this falls back on the default colors
But when I apply direct.label() to the same plot, it overrides the color scale in favor of the ggplot default. Is there a way to prevent this? If not, what's the best way to assign new colors to the default ggplot scale?
Thanks,
Matt
This happens because direct.label(p) operates by adding the label geom to p, then by hiding the color legend, since labeling the colors twice would be redundant. One way to hide the color legend is by adding scale_colour_discrete(legend=FALSE), and this is what I do inside of direct.label. So when directlabels applies scale_colour_discrete, your scale_colour_manual will be lost. The workaround is to use the following idiom:
p <- ggplot(...)
direct.label(p)+
scale_colour_manual(...)

Resources