and I am trying to create a double layered pie, here is my data:
winner <- c("White" , "draw" , "Black")
lowrated_scotch1 <- c(0.56617647, 0.04411765, 0.38970588) #winrate for Whites,Draws,Blacks in chess
highrated_scotch1 <- c(0.50000000, 0.03676471, 0.46323529)
To give more context, i'm trying to visualize the difference in winrate between whites/draws/blacks for a highrated/lowrated players in Chess for the Scotch opening from the data I already managed to gather.
This is what I have in mind :(image taken from google image)
layered pie chart example.
This is my code :
multi_layer_scotch<-data.frame(winner = c("White","draw","Black"),
Y = c(highrated_scotch),
X = c(lowrated_scotch))
ggplot(multi_layer_scotch, aes(x = X, y = Y, fill = winner))+
geom_bar(stat = "identity")+
scale_fill_manual(values = c("#769656","#baca44","#eeeed2"))+
coord_polar(theta="y")+
theme_void()
and this is what i'm getting as an output :
my marvelous not complete graph
As you can see, the graph isn't layered the way I want. The 3 layers from my plot should be assembled in one layer (to represent the lowrated payers) stacked with another layer (representing the highrated players).
I tried to follow the solution given in this post , but I couldn't manage to do it myself, I felt like it was a little incomplete : Multi-level Pie Chart in R
I'de be glad if you could help me with this! thanks in advance
did you mean something like this:
df1 <- melt(multi_layer_scotch)
ggplot(df1, aes(x = variable, y = value, fill = winner))+
geom_bar(stat = "identity")+
coord_polar(theta="y")
Related
I am trying to create line graph with the following code:
ggplot(lnewdat, aes(x = Cl, y = Probability, colour = Level, group = Level)) + geom_smooth(stat = 'identity') + facet_grid(Species~Year)
I get the a graph that looks similar to this graph:
When looking up a solution to the jagged lines, everything mentions setting the "Group". As you can see from my code, I have already done that. Your help is appreciated!
I am having a hard time plotting percentage instead of count when using facet_grid.
I have the following DF (this is an example, my DF is much longer):
'Gu<-c("1","0","0","0","1","0")
variable<-c("THR","Screw removal","THR","THR","THR","Screw removal")
value<-c("0","1","0","1","0","0")
df2<-data.frame(Gu,variable,value)'
and I am trying to plot the "1" values out of the specific variable (either THR or Screw removal) and split the graph by "Gu" (facet grid).
I manage to code it to plot count, but I can seem to be able to calculate the percentage (I need to calculate the percentage from each variable only and not from all the DF)
This is my code:
ggplot(data = df2, aes(x = variable,y =value ,
fill = variable)) +
geom_bar(stat = "identity")+
facet_grid(~ Gu,labeller=labeller(Gu
=c('0'="Nondisplaced fracture",'1'="Displaced
fracture")))+
scale_fill_discrete(name = "Revision", labels =
c("THR","SCREW"))
and this is what I plotted:
enter image description here
I searched this website and the web and couldn't find an answer...
any help will do!
thanks
Have an assignment where we need to provide one-dimensional graphs for EDA but the sample code given answers most of the requirements already (simple scatter and box plots and a histogram) so I am trying to "spice it up" a little by creating some more interesting graphs. Only need a couple.
The data set is the twin IQ data across several studies/authors and I was wanting to do a back-to-back histogram of the twins separated by author. So far I can do an overlay of authors or the back to back of the twins using ggplot but I am then stuck when trying to separate in to either 4 graphs or overlaid back-to-backs.
The code I was using for the overlay was ggplot with either geom_density or geom_histogram and the code for the back-to-back came from R-Bloggers and I used the first snippet:
ggplot(df, aes(IQ)) + geom_histogram(aes(x = x1, y = ..density..), fill = "blue") + geom_histogram( aes(x = x2, y = -..density..), fill = "green")
What I am looking for is a way to combine these two techniques or how to get ggplot to split the graphs up by factor in much the same was as plot/lattice does when you do, for example:
bwplot(y~x1.x2|Author, data=df)
The snippet that I am using to achieve separate plots includes facet_grid() such that the final code is:
ggplot(df, aes(y)) + facet_grid(~Author) + geom_histogram(aes(x = x1, y = ..density..), fill = "green") + geom_histogram(aes(x = x2, y = -..density..), fill = "blue")
I wasn't previously aware of the facet_grid() function of ggplot so thank you very much to MLavoie and Brandon Bertelsen.
In the quantmod package, chartSeries provides the option to "addCCI" which is based on a moving average. The CCI chart uses a very good idea but is based on a moving average whereas I prefer to use a single long term average which is why Im trying to replicate the chart in ggplot2 instead of in quantmod.
the addCCI picture I want to replicate is the bottom chart
I want to use the same concept of using a highlighted area for the range outside of the +100/-100 but Id like to highlight points above and below 1 standard deviation from the long term mean. In the picture below.
ideally I want the area above and below 1 std dev to appear the same as in the addCCI chart
library(quantmod)
library(ggplot2)
y <- rnorm(31,2,1)
x <- seq.Date(as.Date("2015-01-01"),as.Date("2015-01-31"),1)
d<-data.frame(cbind(x,y))
d$pmean <- mean(d$y)
d$m1d <- mean(d$y)-sd(d$y)
d$p1d <- mean(d$y)+sd(d$y)
d2 <- xts(d[,-1],order.by=as.Date(d$x))
chartSeries(d2$y, TA="addCCI(5)")
ggplot(d, aes(as.Date(x))) +
geom_line(aes(y = y, colour = "blue")) +
geom_line(aes(y = pmean, colour = "red")) +
geom_line(aes(y = p1d, colour = "red")) +
geom_line(aes(y = m1d, colour = "red"))
This bit of code is what I used to create the examples above.
Is this even possible in ggplot2 and if so, I would appreciate any help in accomplishing it.
Thank you in advance
I have following code to graph a contracts in different countries.
Country <- CCOM$Principal.Place.of.Performance.Country.Name
Val <- CCOM$Action_Absolute_Value
split <- CCOM$Contract.Category
ggplot(CCOM, aes(x = Country, y = Val, fill = levels(split))) +
geom_bar(stat = "identity")
I want a simple stacked bar chart with the bars colored by the contract category which is the variable "split" (ie. CCOM$Contract.Category).
However when I run the code it produces the graph below:
Why won't gplot separate the spending into three distinct blocks? Why do I get color sections scattered throughout the chart.? I have tried using factor(split) and levels(split) but does not seem to work. Maybe I am putting it in the wrong position.
Ah, I just realized what was going on. You seem scared to modify your data frame, don't be! Creating external vectors for ggplot is asking for trouble. Rather than create Country and Val as loose vectors, add them as columns to your data:
CCOM$Country <- CCOM$Principal.Place.of.Performance.Country.Name
CCOM$Val <- CCOM$Action_Absolute_Value
Then your plot is nice and straightforward, you don't have to worry about order or anything else.
ggplot(CCOM, aes(x = Country, y = Val, fill = Contract.Category)) +
geom_bar(stat = "identity")
as you suggest order provides a solution:
ggplot(CCOM[order(CCOM$split), ], aes(x = Country, y = Val, fill = Contract.Category)) +
geom_bar(stat = "identity")
I have a similar example where I use the equivalent of fill as Contact.Category and it still requires the reordering.