ggplot bubble graph custom colors - r

I have this dataframe that I'm trying to use custom colors
data <- data.frame(condition=c('1','1','1','1','1','2','2','2','2','2','3','3','3','3','3'), AssessmentGrade=c('400','410','420','430','440','500','510','520','530','540','300','310','320','330','340'), Freq=c('1','2','1','5','7','9','1','5','3','4','5','8','1','3','5'), MathGrade=c('A+','B-','C-','D','F','A-','B','C+','D-','F','A+','D','D','F','C'), Condition=c('Condition 1','Condition 1','Condition 1','Condition 1','Condition 1','Condition 2','Condition 2','Condition 2','Condition 2','Condition 2','Condition 3','Condition 3','Condition 3','Condition 3','Condition 3'))
I used ggplot to get abubble graph but I was wondering how I would edit it to use my company's standard colors
p <- ggplot(data, aes(x = MathGrade, y = AssessmentGrade, size = Freq, fill = Condition)) +
geom_point(aes(colour = Condition)) +
ggtitle("Main Title") +
labs(x = "First Math Grade", y = "Math Assessment Score")
I have a vector called colors:
colors
[1] "#101820" "#AF272F" "#EAAA00"
and I tried to graph it with this:
p <- p + scale_fill_manual(values = color)
nothing changed. I tried following directions here but nothing changed. Can someone assist?

You create a palette:
my_colors<- c("#101820", "#AF272F", "#EAAA00")
Then when it comes time you use that in your plot:
p <- ggplot(data, aes(x = MathGrade, y = AssessmentGrade, size = Freq, fill = Condition)) +
geom_point(shape=21) +
ggtitle("Main Title") +
labs(x = "First Math Grade", y = "Math Assessment Score") +
scale_fill_manual(values=my_colors) #or you could enter the color numbers directly here
This worked during testing but ggplot does not like how you used size in the main aesthetic.

Related

How to highlight a column in ggplot2

I have the following graph and I want to highlight the columns (both) for watermelons as it has the highest juice_content and weight. I know how to change the color of the columns but I would like to WHOLE columns to be highlighted. Any idea on how to achieve this? There doesn't seems to be any similar online.
fruits <- c("apple","orange","watermelons")
juice_content <- c(10,1,1000)
weight <- c(5,2,2000)
df <- data.frame(fruits,juice_content,weight)
df <- gather(df,compare,measure,juice_content:weight, factor_key=TRUE)
plot <- ggplot(df, aes(fruits,measure, fill=compare)) + geom_bar(stat="identity", position=position_dodge()) + scale_y_log10()
An option is to use gghighlight
library(gghighlight)
ggplot(df, aes(fruits,measure, fill = compare)) +
geom_col(position = position_dodge()) +
scale_y_log10() +
gghighlight(fruits == "watermelons")
In response to your comment, how about working with different alpha values
ggplot(df, aes(fruits,measure)) +
geom_col(data = . %>% filter(fruits == "watermelons"),
mapping = aes(fill = compare),
position = position_dodge()) +
geom_col(data = . %>% filter(fruits != "watermelons"),
mapping = aes(fill = compare),
alpha = 0.2,
position = position_dodge()) +
scale_y_log10()
Or you can achieve the same with one geom_col and a conditional alpha (thanks #Tjebo)
ggplot(df, aes(fruits, measure)) +
geom_col(
mapping = aes(fill = compare, alpha = fruits == 'watermelons'),
position = position_dodge()) +
scale_alpha_manual(values = c(0.2, 1)) +
scale_y_log10()
You could use geom_area to highlight behind the bars. You have to force the x scale to discrete first which is why I've used geom_blank (see this answer geom_ribbon overlay when x-axis is discrete) noting that geom_ribbon and geom_area are effectively the same except geom_area always has 0 as ymin
#minor edit so that the level isn't hard coded
watermelon_level <- which(levels(df$fruits) == "watermelons")
AreaDF <- data.frame(fruits = c(watermelon_level-0.5,watermelon_level+0.5))
plot <- ggplot(df, aes(fruits)) +
geom_blank(aes(y=measure, fill=compare))+
geom_area(data = AreaDF, aes( y = max(df$measure)), fill= "yellow")+
geom_bar(aes(y=measure, fill=compare),stat="identity", position=position_dodge()) + scale_y_log10()
Edit to address comment
If you want to highlight multiple fruits then you could do something like this. You need a data.frame with where you want the geom_area x and y, including dropping it to 0 between. I'm sure there's slightly tidier methods of getting the data.frame but this one works
highlight_level <- which(levels(df$fruits) %in% c("apple", "watermelons"))
AreaDF <- data.frame(fruits = unlist(lapply(highlight_level, function(x) c(x -0.51,x -0.5,x+0.5,x+0.51))),
yval = rep(c(1,max(df$measure),max(df$measure),1), length(highlight_level)))
AreaDF <- AreaDF %>% mutate(
yval = ifelse(floor(fruits) %in% highlight_level & ceiling(fruits) %in% highlight_level, max(df$measure), yval)) %>%
arrange(fruits) %>% distinct()
plot <- ggplot(df, aes(fruits)) +
geom_blank(aes(y=measure, fill=compare))+
geom_area(data = AreaDF, aes(y = yval ), fill= "yellow")+
geom_bar(aes(y=measure, fill=compare),stat="identity", position=position_dodge()) + scale_y_log10()
plot

Plotting Gradient for Line chart using ggplot_line()

Below is the a small dataset which i have tried to reproduce it to my best understanding.As in the attached plot you can observe that i am able to gradient in geom_point() but the same visuals i am trying for geom_line.Note : we are always been provided with data ofTempandvar.Thecat` variable is not given in data set.
df=data.frame(seq=(1:30),Temp =rnorm(30,mean = 34 ,sd=18))
f=summary(df$Temp)
df$cat <- cut(df$Temp,
breaks=c(f[1], f[3] ,f[4] ,f[6]),
labels=c("low","medium","high"))
f=ggplot(df , aes(x=seq ,y= Temp,colour=cat))+ geom_line()
f
Output of Above Code
Required Output
Gradient should as per High , Medium & Low using geom_line() function in ggplot2.
You want to group the lines by cat, but colour them by Temp. For getting a nice gradient, I like the colour scales in the viridis package, but you can play around with scale_colour_gradient instead:
library(viridis)
ggplot(df , aes(x=seq ,y= Temp,colour=Temp, group = cat)) +
geom_line(size = 1.2) +
scale_colour_viridis(option = "A")
Output:
To have a legend for the lines, you can represent cat with something like
shape:
ggplot(df , aes(x=seq ,y= Temp,colour=Temp, group = cat, shape = cat)) +
geom_point(size = 3) +
geom_line(size = 1.2) +
scale_colour_viridis(option = "A")
Is this what you want:
a <- data.frame(seq=(1:30),Temp =rnorm(30,mean = 34 ,sd=18))
ggplot(a, aes(x = seq, y = Temp, color = Temp )) +
geom_line(size = 0.5) +
geom_smooth(aes(color=..y..), size=1.5, method = "loess", se=FALSE) +
scale_colour_gradient2(low = "green", mid = "yellow" , high = "red",
midpoint=median(a$Temp))

Adding specific labels to legend

Goal: add numeric labels(number of bar on the plot) to legend, e.g 1.Company X
2.Company Y
3.Company Z
library(ggplot2)
require(scales)
companies = c('Company X','Company Y','Company Z')
profits = c(100,200,300)
data1 = data.frame(companies,profits)
CP <- ggplot(data1, aes(x = data1$companies,y = data1$profits,fill =data1$companies )) +
geom_bar(stat = 'identity') +
scale_x_discrete(name = "Companies",labels = 1:length(data1$companies))
Currently I have
What you might be looking for is paste:
newcompanies <- paste(1:50,companies,sep=".")
Personal (style) suggestion:
Search how you can change the colours to one colour-range. For fifty companies this colour-range will be confusing.
You can use seq_along and paste to make the plot you want as follow:
ggplot(data1, aes(x=seq_along(companies), weight=profits,
fill=paste(seq_along(companies), companies))) +
geom_bar() +
scale_fill_discrete("Companies") +
labs(x="Company Labels", y="Count")

Reuse ggplot layers in multiple plots

I am plotting tons of graphs which essentially use the same type of formatting. Just wondering if it possible to store these layers in a variable and reuse them.
Approach 1 (does not work)
t <- layer1() + layer2()
ggplot(df,aes(x,y)) + t
Approach 2 (works but not very elegant)
t <- function(x) x + layer1() + layer2()
t(ggplot(df,aes(x,y))
Any suggestion along the lines of approach 1?
Thanks!
While I wait for some clarification, here are a few examples that demonstrate how to add previously created layers to an existing plot:
p <- ggplot(mtcars,aes(x = cyl,y = mpg)) +
geom_point()
new_layer <- geom_point(data = mtcars,aes(x = cyl,y = hp),colour = "red")
new_layer1 <- geom_point(data = mtcars,aes(x = cyl,y = wt),colour = "blue")
p + new_layer
p + list(new_layer,new_layer1)
Based on the Joran's answer, I now put my layers into a list, and add it in my plots. Works like a charm :
r = data.frame(
time=c(5,10,15,20),
mean=c(10,20,30,40),
sem=c(2,3,1,4),
param1=c("A", "A", "B", "B"),
param2=c("X", "Y", "X", "Y")
)
gglayers = list(
geom_point(size=3),
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem), width=.3),
scale_x_continuous(breaks = c(0, 30, 60, 90, 120, 180, 240)),
labs(
x = "Time(minutes)",
y = "Concentration"
)
)
ggplot(data=r, aes(x=time, y=mean, colour=param1, shape=param1)) +
gglayers +
labs(
color = "My param1\n",
shape = "My param1\n"
)
ggplot(data=r, aes(x=time, y=mean, colour=param2, shape=param2)) +
gglayers +
labs(
color = "My param2\n",
shape = "My param2\n"
)
I know this is old, but here is one that avoids the clunky t(ggplot(...)))
t<-function(...) ggplot(...) + layer1() + layer2()
t(df, aes(x, y))

Change the colour palette in histogram

I am trying to change the colours of my histogram, but not sure how to do it, that's my code:
qplot(user, count, data=count_group, geom="histogram", fill=group,
xlab = "users", ylab="count",
main="Users")+
opts(axis.text.x=theme_text(angle=90, hjust=0, size=7))
here is the histogram I get, but the default colours are too bright,
I would like to use colours like this
I tried to add the line, but it didnt work.
scale_fill_brewer(palette = palette)
If you want to use the Brewer Set1 with that many groups, you could do something like this:
library(ggplot2)
count_group <- data.frame(user=factor(rep(1:50, 2)),
count=sample(100, 100, replace=T),
group=factor(rep(LETTERS[1:20], 5)))
library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(9, "Set1"))
ngroups <- length(unique(count_group$group))
qplot(user, count, data=count_group, geom="histogram", fill=group,
xlab = "users", ylab="count") +
opts(axis.text.x=theme_text(angle=90, hjust=0, size=7)) +
scale_fill_manual(values = cols(ngroups))
EDIT
You can create and use multiple colorRampPalettes, e.g. to assign blues to groups A to J and reds to groups K to T:
blues <- colorRampPalette(c('dark blue', 'light blue'))
reds <- colorRampPalette(c('pink', 'dark red'))
qplot(user, count, data=count_group, geom="histogram", fill=group,
xlab = "users", ylab="count") +
opts(axis.text.x=theme_text(angle=90, hjust=0, size=7)) +
scale_fill_manual(values = c(blues(10), reds(10)))
# blues(10) and reds(10) because you want blues for the first ten
# groups, and reds thereafter. Each of these functions are equivalent
# to providing vectors containing ten hex colors representing a gradient
# of blues and a gradient of reds.
An update on jbaums's answer. As far as I can make out, with the new ggplot2 (as of March 2014), the following syntax is available:
p <- qplot(user, count,
data = count_group,
geom = "histogram",
stat = "bin2d",
fill = group,
xlab = "users",
ylab = "count"
)
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 0, size = 7))
p <- p + scale_fill_manual(values = cols(ngroups))
p
This was a little long for a comment, but it's not a full answer, the rest of the code is as given by jbaums, who must be thanked!

Resources