I've no idea where to even start with this. I've looked at GGPlot and plotly etc to try and find the right thing but haven't come across anything.
This is as example of my data though
Skill <- c("Tackling", "Shooting", "Technique", "Passing", "Pace", "Stamina")
Grade <- c("A", "C", "C", "B", "A", "B")
data <- data.frame(Skill, Grade)
This is the sort of graph I'd like
I'm a football scout and it would be fantastic to be able to have a graph like that to compare the players we have to the player I'm scouting.
so if the grade is D, it would just show red, if the grade was C it would show red and orange. Etc.
This is quite close to what you want:
Skill <- c("Tackling", "Shooting", "Technique", "Passing", "Pace", "Stamina")
Grade <- c("A", "C", "C", "B", "A", "B")
data <- data.frame(Skill, Grade)
library(ggplot2)
library(dplyr)
data$grade <- factor(data$Grade, levels=c("D","C","B","A"))
data$grade2 <- recode(data$grade, A="B")
data$grade3 <- recode(data$grade2, B="C")
data$grade4 <- recode(data$grade3, C="D")
ggplot(data, aes(x=Skill, y=grade)) +
geom_bar(stat="identity", fill="green",col="black",width=1) +
geom_bar(aes(y=grade2),stat="identity", fill="yellow",col="black",width=1) +
geom_bar(aes(y=grade3),stat="identity", fill="orange",col="black",width=1) +
geom_bar(aes(y=grade4),stat="identity", fill="red",col="black",width=1) +
scale_y_discrete(limits = c("D","C","B","A")) +
coord_polar(start = pi/6) + theme_bw() + theme(axis.text.y = element_blank()) +
theme(axis.ticks = element_blank(), axis.title = element_blank())
How about this
library(ggplot2)
ggplot(data = data, aes(Skill, Grade, fill = Grade)) +
geom_tile() +
coord_polar() +
theme_bw()
To have all levels below the grade coded, you'll need to have all those lower levels within the dataframe, which is in a way redundant. Wouldn't it be?
d = transform(data, gr = as.numeric(factor(data$Grade, c("D", "C", "B", "A"))))
d = do.call(rbind, lapply(split(d, d$Skill), function(x){
foo = with(x, setNames(data.frame(Skill[1], Grade[1], seq(gr)), names(x)))
}))
library(ggplot2)
ggplot(d, aes(Skill, gr, fill = factor(gr, 4:1))) +
geom_col() +
coord_polar()
Related
I coloured my data by "Full Bath", however it is a similar shade & unclear.
I am trying to change the colour by variable of "Full Bath" to be more clear.
Could anyone help :)
This is what I have:
data %>%
ggplot(aes(Sale.Price, Total.Bsmt.SF)) +
geom_point(aes(colour = Full.Bath)) + geom_smooth()
To manually change the colors, you can use scale_color_manual, as follows:
### Simulation of data
set.seed(1)
data <- data.frame(Sale.Price=rnorm(100, 100, 10),
Total.Bsmt.SF=rnorm(100, 10, 1),
Full.Bath=rep(c("a", "b", "c", "d", "e"), 20))
### Plot data
data %>%
ggplot(aes(x = Sale.Price, y = Total.Bsmt.SF)) +
geom_point(aes(colour = Full.Bath)) +
geom_smooth() +
scale_color_manual(values=c("#84d58d", "#84c1d5", "#9e84d5", "#d584b0", "#d5be84"))
Link for colors selection
https://htmlcolorcodes.com/fr/
I'm new to R and I'm trying to create a single plot with data from 2 melted dataframes.
Ideally I would have a legend for each of the dataframes with their respective titles; however, I get a only a single legend with the title of the first aesthetic.
My starting point is:
aerobic_melt <- melt(aerobic, id.vars = 'Distance', variable.name = 'Aerobic')
anaerobic_melt <- melt(anaerobic, id.vars = 'Distance', variable.name = 'Anaerobic')
plot <- ggplot() +
geom_line(data = aerobic_melt, aes(Distance, value, col=Aerobic)) +
geom_line(data = anaerobic_melt, aes(Distance, value, col= Anaerobic)) +
xlim(0, 125) +
ylab('Energy (J/kg )') +
xlab('Distance (m)')
Which results in
I've searched, but with my limited ability I haven't been able to find a way to do it.
My question is:
How do I create separate legends with titles 'Aerobic' and 'Anaerobic' which should respectively refer to A,B,C,F,G,L and E,H,I,J,K?
Any help is appreciated.
Obviously we don't have your data, but I have created some sample data that should have the same names and structure as your own data frames, since it works with your own plot code. See the end of the answer for the data used here.
You can use the package ggnewscale if you want two color scales on the same plot. Just add in a new_scale_color() call between your geom_line calls. I have left the rest of your code as-is.
library(ggplot2)
library(ggnewscale)
plot <- ggplot() +
geom_line(data = aerobic_melt, aes(Distance, value, col=Aerobic)) +
new_scale_color() +
geom_line(data = anaerobic_melt, aes(Distance, value, col= Anaerobic)) +
xlim(0, 125) +
ylab('Energy (J/kg )') +
xlab('Distance (m)')
plot
Data
set.seed(1)
aerobic_melt <- data.frame(
Aerobic = rep(c("A", "B", "C", "F", "G", "L"), each = 120),
value = as.numeric(replicate(6, cumsum(rnorm(120)))),
Distance = rep(1:120, 6))
anaerobic_melt <- data.frame(
Anaerobic = rep(c("E", "H", "I", "J", "K"), each = 120),
value = as.numeric(replicate(5, cumsum(rnorm(120)))),
Distance = rep(1:120, 5))
I have a dataset, named “data”:
df=ddply(data,c("Treatment","Concentration"),summarise,mean=mean(Inhibition),sd=sd(Inhibition),n=length(Inhibition),se=sd/sqrt(n))
p <- ggplot(df, aes(x=Treatment, y=Inhibition))
p1 <- p + geom_bar(stat="identity", position="dodge") +
geom_errorbar(aes(ymin=Inhibition-se,ymax=Inhibition+se), position="dodge",width=0.2)
and I got the following graph:
I want x-axis to be like the picture below:
How woud I do this??
This is best achieved using a facet within ggplot. As you haven’t included a reusable dataset, I have made one here:
df <- data.frame(Group = c("A", "A", "A", "A", "B"),
SubGroup = c(letters[1:5]),
value = 1:5
)
See below the facet_grid line which has a few additional options specified. You can read more about the added arguments here
library(ggplot2)
ggplot(df, aes(x = SubGroup, value)) +
geom_bar(stat="identity", position="dodge") +
facet_grid(.~Group, scales = "free_x", space = "free", switch = "x") +
theme(strip.placement = "outside")
For your data, you will need to split the drug and dose into two separate columns first, like my example.
Let's say I have the following data frame
category = c( "A", "B", "C", "B", "A", "B")
Jan.10 = c(20,10,30,20,15,46)
Feb.10 = c(10,10,40,20,25,46)
Mar.10 = c(40,10,80,20,25,56)
Jan.11 = c(50,10,20,20,45,56)
Feb.11 = c(70,10,30,20,35,46)
Mar.11 = c(60,10,50,20,25,66)
df <- data.frame(category, Jan.10, Feb.10, Mar.10, Jan.11, Feb.11, Mar.11)
How do I create a three histograms by faceting, one for each category, with months on the x-axis and counts on the y-axis?
Something like this I am trying to get:
example-image
This is my best attempt so far:
df_melt <- df %>% melt()
df_melt$variable <- as.yearmon(df_melt$variable, "%b.%y")
ggplot(df_melt, aes(x=variable)) +
geom_histogram() +
xlab("Year and Month") +
ylab("Sales") +
facet_grid(category ~.) +
theme_bw()
But it isn't working right.
Any help would be much appreciated!
I think you want something like this:
df_melt <- df %>% melt()
df_melt$variable <- as.yearmon(df_melt$variable, "%b.%y")
ggplot(df_melt, aes(x=variable, y=value)) +
geom_bar(stat='identity') +
xlab("Year and Month") +
ylab("Sales") +
facet_wrap(~category, ncol = 1) +
theme_bw()
I'm trying to make a plot with horizontal boxplots in ggplot2, which you can only do by using coord_flip(). I'm also trying to space the boxplots vertically to group certain sets together. I have read that faceting is recommended for this kind of thing, but this is incompatible with coord_flip(), as we can see here: ggplot2: boxplot with facet_grid and free scale. So I'm wondering if it's possible to use blank levels in order to create blank spaces. Here's what I've managed to do so far:
d <- diamonds
library("ggplot2")
levels(d$cut) <- list(A="Fair", B="Good", "-", C="Very Good", D="Ideal", E="Premium")
p = ggplot(d, aes(x=cut, y=depth))
p +
geom_boxplot(color="black", size=0.2) +
theme_bw() +
scale_x_discrete(breaks = c("A", "B", "-", "C", "D", "E"), drop=FALSE) +
coord_flip()
ph = 2.75
pw = 4
ggsave("plot.png", height=ph, width=pw)
As you can see, if I create a blank level with "-" in it and include it in scale_x_discrete(), then somehow I get a blank row. The problem is I can only add one space. Does anyone have any ideas on how to add spaces between these horizontal box plots?
Here's one way that will let you add more blank levels:
d <- diamonds
levels(d$cut) <- list(A="Fair", B="Good", " "="space1", C="Very Good", D="Ideal", " "="space2", E="Premium")
ggplot(d, aes(x=cut, y=depth)) +
geom_boxplot(color="black", size=0.2) +
theme_bw() +
scale_x_discrete(breaks = c("A", "B", " ", "C", "D", " ", "E"), drop=FALSE) +
coord_flip()
This will leave tick marks on the spacers as well, though:
Removing all ticks is simple by adding:
+ theme(axis.ticks.y = element_line(linetype=0))
But if you want to remove the tick marks only for the spacers, at least one way (I'm sure there others) is to do it with a custom function:
f <- function(x) {
x[!x %in% c("A", "B", "C", "D", "E")] <- NA
x
}
ggplot(d, aes(x=cut, y=depth)) +
geom_boxplot(color="black", size=0.2) +
theme_bw() +
scale_x_discrete(breaks=f, drop=FALSE) +
coord_flip()