How to put more information on my R Pie Chart ? - r

I have the following simple R code.
vect <- c(a = 4, b = 7, c = 5)
pie(vect, labels = c("A", "B", "C"), col = c("#999999", "#6F6F6F", "#000000"))
that print the following pie chart
How do I modify the above code to print more detail on my pie chart, so that it look like this other one ?
Thanks for any reply !

My above comment in details:
Create a dummy data frame for the example:
vect <- data.frame(v=c(rep('a', 25), rep('b', 30), rep('c', 45)))
Call ggplot for a pie-chart (coord_polar) based on the manual:
p <- ggplot(vect, aes(x=factor(1), fill = factor(v))) + geom_bar(width = 1) + coord_polar(theta="y")
Tweak it:
p + opts(title = "Decision tree") + xlab('') + ylab('') +
theme_bw() + scale_fill_grey(name = "Rankings")
Resulting in:

I would recommend several attributes you dive into with the plot method. For the plot title, see main attribute, and legend attribute for adding the colorful palette.
You may also refer to a simple plot tutorial here: http://www.harding.edu/fmccown/r/#piecharts

Related

Bubble chart in R

all I have used this script to get this bubble plot
library(reader)
data <- read.csv
r <- c(1,194,26302,81,69, 12,11617,55,10)
dfdata = data.frame(data,r)
dfdata
sizeRange <- c(5,20)
library(ggplot2)
ggplot(data, aes(x=0,ï..Phylum)) +
geom_point(aes(size = r,alpha = 7,colour = "red", stroke=2)) +
scale_size(range = sizeRange) +
theme_minimal()+
theme(legend.position = "none")
I am trying to reduce the axis distance for this plot. How that can be done. And also, these bubble size is not coming properly according to the "r" values.
Please help.
Thank you

Add separate legend to PDF from lapply

I've created a multi-page PDF with plots generated from a few hundred unique identifiers. Basically, I would like to add a separate legend panel once per page.
The PDF is basically constructed as detailed here and here
There are dozens of walk-throughs on how to add a separate legend for a few graphical objects using grid.arrange, the most promising are here and here.
Basically, the steps are:
create the database
use lapply to make a list of the graphical objects and
create a pdf the chunks up the list of graphical objects.
I suspect the process falls apart at step 3 - adding the legend to the list of grobs.
To reproduce the problem
color.names <- setNames(c("A", "B", "C", "D", "F"), c("green3", "chocolate1", "darkgoldenrod1", "firebrick1"))
group.colors <- c(A = "#333BFF", B = "#CC6600", C ="#9633FF", D = "#E2FF33", F = "#E3DB71")
SOexample <- data.frame(
studentid = runif(100,min=500000, max=999999),
grade = runif(100, min=20, max=100),
lettergrade = sample(c("A", "B","C","D","F"),size=100,replace=TRUE),
firstname = sample(c("Alan", "Billy","Charles","Donna","Felicia"),size=100,replace=TRUE)
)
To generate the legend
df <- SOexample
gl <- ggplot(df, aes(x=" ", y=as.numeric(grade), ymin=50, ymax=100))+ geom_boxplot()+ guides(fill=FALSE) + geom_point(aes(colour=lettergrade)) + labs( x=" ", y=" ") + ggtitle(sprintf("%s", df$firstname), aes(cex=.05)) + scale_colour_manual(name="Number", values=group.colors) + scale_fill_manual(name="", values="red") + theme_grey() + theme(legend.position="none", plot.title = element_text(size = 8, face = "bold"), plot.subtitle=element_blank()) + theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())
The function to grab the legend using cowplot
install.packages("cowplot")
library(cowplot)
leg <- get_legend(gs + theme(legend.position="right"))
To make all the graphical objects
plist = lapply(split(SOexample, factor(SOexample$studentid)), function(df) { ggplot(df, aes(x=" ", y=as.numeric(grade), ymin=50, ymax=100))+ geom_boxplot()+ guides(fill=FALSE) + geom_point(aes(colour=lettergrade)) + labs( x=" ", y=" ") + ggtitle(sprintf("%s", df$firstname), aes(cex=.05)) + scale_colour_manual(name="Number", values=group.colors) + scale_fill_manual(name="", values="red") + theme_grey() +theme(legend.position="none", plot.title = element_text(size = 8, face = "bold"), plot.subtitle=element_blank()) + theme(axis.title.x=element_blank(),axis.text.x=element_blank(), axis.ticks.x=element_blank())})
Making the PDF
pdf("allpeople.pdf", pointsize=8)
for (i in seq(1, length(plist), 11)) {
grid.arrange(grobs=plist[i:(i+11)],
ncol=4, left="Magic Numbers", bottom=" ")
}
dev.off()
I suspect the process is falling apart in the create PDF stage. Ideally, I would add the legend as a graphical object in / at the grid.arrange step, e.g.,
grobs[12]<- leg
But no luck, and also the last item in the plist() process seems to have not been fully converted to a graphical object.
Using this auto-generating method, i.e., cannot list out graphical objects individually, how does one add the legend to each page of the PDF?
There are various options (ggsave('file.pdf',marrangeGrob(plist,ncol=4,nrow=3)), for instance), but I'd probably do something like this for finer control:
pl <- split(plist, gl(10,10))
pdf("allpeople.pdf", pointsize=8)
for (i in seq_along(pl)) {
grid.arrange(grobs=c(pl[[i]], list(leg)),
ncol=4,
left="Magic Numbers",
bottom=" ")
}
dev.off()

Indexes withing ggplot in R

I am generating some plots with the following code.I have 8 plots generated with the the following code and what I want is to have them on the same page with no titles. More specifically, I want in every plot to have on the left up-corner a letter (a,b..) and at the end of the plot to have something like an one-row legend (e.g Plots: a. category one, b. category two, ...).
Code:
g1= ggplot(som, aes(x=value, y=variable))+geom_smooth(method=lm,alpha=0.25,col='green',lwd=0.1) +ylim(0,1000)+xlim(-2,2)+
geom_point(shape=23,fill="black",size=0.2)+theme_bw()+theme(plot.background = element_blank(),panel.grid.major = element_blank()
,panel.grid.minor = element_blank()) +labs(x="something here",y="something else")+
theme(axis.title.x = element_text(face="bold", size=7),axis.text.x = element_text(size=5))+
theme(axis.title.y = element_text(face="bold", size=7),axis.text.y = element_text(size=5))+
theme(plot.title = element_text(lineheight=.8, face="bold",size=8))
grid.arrange(g1,g2,g3,g4,g5,g6,g7,g8,ncol=2)
Is it possible to do that with ggplot? If so, how can I do this?
p.s I have no problem with the above code
Thank you.
This is how you could do it with library(cowplot).
First some plots:
set.seed(1)
plots <- list()
for (i in 1:8) {
my_cars <- mtcars[sample(1:nrow(mtcars), 10), ]
plots[[i]] <- ggplot(my_cars, aes(mpg, hp, color = as.factor(cyl))) +
geom_point() +
geom_smooth(method = "lm", color = "black")
}
Then to have a unifying title (or legend here) we use a combination of two plot_grid() calls.
lbls <- LETTERS[1:length(plots)]
# add a line break because its long
lbls <- gsub("E", "\nE", lbls)
grid <- plot_grid(plotlist = plots, labels = lbls, ncol = 2)
legend <- ggdraw() +
draw_label(paste0(lbls, "= category",1:length(plots), collapse = " "))
plot_grid(grid, legend, rel_heights = c(1, .1), ncol = 1)
The documentation for cowplot is great and has a ton of examples. Check it out here and here. Let me know if you get stuck.

Adding a legend to a double plot using ggplot

I'm trying to add a legend to my plot using ggplot in R. Everything OK so far. My case is special because I'm trying to deal with three variables, but not in order to draw a 3D plot but draw a 2D plot facing v1 vs. v2 and v1 vs. v3.
I get my plot in a correct way but I dont get the legend.
This is my code:
colfuncWarmest <- colorRampPalette(c("orange","red"))
colfuncColdest <- colorRampPalette(c("green","blue"))
plot <- ggplot(data=temperatures_Celsius, aes(x=temperatures_Celsius$Year))
params <- labs(title=paste("Year vs. (Warmest minimum temperature\n",
"and Coldest minimum temperature)"),
x="Year",
y="Coldest min temp / Warmest min temp")
theme <- theme(plot.title = element_text(hjust = 0.5)) #Centering title
wmtl<-geom_line(data=temperatures_Celsius,
aes(y=temperatures_Celsius$Warmest.Minimum.Temperature..C.,
color="red"
),
colour=colfuncWarmest(length(temperatures_Celsius$Year))
)
wmtt<-stat_smooth(data=temperatures_Celsius,
aes(y=temperatures_Celsius$Warmest.Minimum.Temperature..C.),
color="green",
method = "loess")
cmtl<- geom_line(data=temperatures_Celsius,
aes(y=temperatures_Celsius$Coldest.Minimum.Temperature..C.,
color="blue"
),
colour=colfuncColdest(length(temperatures_Celsius$Year))
)
cmtt<-stat_smooth(data=temperatures_Celsius,
aes(y=temperatures_Celsius$Coldest.Minimum.Temperature..C.),
color="orange",
method = "loess")
plot + theme + params + wmtl + wmtt + cmtl + cmtt
(Not all code was added because I did a lot of changes. It is only to get an idea) I get this:
If I add
+ scale_color_manual(values=c("red","blue"))
(for example) in order to add the legend, I get no error, but nothing different happens. I get the same plot.
What I want is only two lines. A red one that says "Warmest minimum" and another blue line that says "Coldest minimum". What could I do to get my legend in this way?
Thanks in advance.
Generally I would say that the correct way to apply a legend to a ggplot is to map a variable to an aesthetic (such as fill, color, size, alpha). Usually this consists of transforming the data to long format (key ~ value pair) and mapping the key variable to color or other aestetic.
In the current case this is not desirable since there is next to no chance the color gradient (colorRampPalette) on the line could be achieved. So I suggest a hacky way where a dummy layer (layer which will not be seen on the plot) is used to create the legend.
Here is some data
temperatures_Celsius = data.frame(year = 1900:2000,
Warmest = rnorm(100, mean = 20, sd = 5),
Coldest = rnorm(100, mean = 10, sd = 5))
Your plot:
colfuncWarmest <- colorRampPalette(c("orange","red"))
colfuncColdest <- colorRampPalette(c("green","blue"))
plot <- ggplot(data=temperatures_Celsius, aes(x=year))
params <- labs(title=paste("Year vs. (Warmest minimum temperature\n",
"and Coldest minimum temperature)"),
x="Year",
y="Coldest min temp / Warmest min temp")
theme <- theme(plot.title = element_text(hjust = 0.5)) #Centering title
wmtl<-geom_line(data=temperatures_Celsius,
aes(y=Warmest),
colour=colfuncWarmest(length(temperatures_Celsius$year)))
wmtt<-stat_smooth(data=temperatures_Celsius,
aes(y=Warmest),
color="green",
method = "loess")
cmtl<- geom_line(data=temperatures_Celsius,
aes(y=Coldest),
colour=colfuncColdest(length(temperatures_Celsius$year)))
cmtt<-stat_smooth(data=temperatures_Celsius,
aes(y=Coldest),
color="orange",
method = "loess")
plot1 <- plot + theme + params + wmtl + wmtt + cmtl + cmtt
Now add a dummy layer:
plot1+
geom_line(data = data.frame(year = c(1900, 1900),
group = factor(c("Coldest", "Warmest"), levels = c("Warmest", "Coldest")),
value = c(10, 20)), aes(x=year, y = value, color = group), size = 2)+
scale_color_manual(values=c("red","blue"))

Limit the color variation in R using scale_color_grey

Before I start, allow me to explain my graph: I have two Genotypes (WTB and whd) and each have two conditions (0 and 7), so I have four lines.
Now, I want to make a plot where each variable and its condition is the same color. Anything with whd will be black and anything with WTB will be grey.
I managed to make the graph with the solid/dashed lines correctly, however, I am having trouble with the coloring. I can only make it so that there is a grey gradient. Are there any parameters for scale_color_grey to help me with this?
This is my code (I took out parts of it for easier read):
ggplot(data=df4, aes(x=Day, y=Remain, group=Condition, shape=Condition, colour=Condition) +
theme_bw() +
geom_line(aes(linetype=Condition), size=1) +
geom_point(size=0, fill="#FFFFFF") +
scale_linetype_manual(name="Genotype",
values=c("solid", "dashed", "solid", "dashed")) +
scale_shape_manual(name="Genotype", values=c(1,19,1,19)) +
scale_color_grey(name="Genotype")
I think this code should produce the plot you want. However, without your exact dataset, I had to generate simulated data.
## Generate dummy data and load library
library(ggplot2)
df4 = data.frame(Remain = rep(0:1, times = 4),
Day = rep(1:4, each = 2),
Genotype = rep(c("wtb", "whd"), each = 4),
Condition = rep(c("0", "7"), times = 4 ))
Next, I created a grouping variable:
df4$both = paste0(df4$Genotype, df4$Condition)
And last, I plotted and saved the figure:
example <- ggplot(data=df4, aes(x=Day, y=Remain,
group=both,
color = Genotype,
linetype = Condition)) +
geom_line() +
scale_color_manual(values = c("grey50", "black")) +
scale_linetype_manual(values = c("solid", "dashed")) +
theme_bw()
ggsave("example.jpg", example)
From your code, it was not apparent why you included the geom_point. Also, if you only want one sub-legend, rather than two, this SO question provides details on how to do that.

Resources