I am trying to draw pie charts using R with the following code. The headings are far from the pie charts. I would like to get the pie charts just below the headings. How can I do that?
x <- c(632,20,491,991,20)
y <- c(37376,41770,5210,5005,3947)
names <- c("alpha","beta","gamma","delta","omega")
par(mfrow=c(1,2))
pie(x, names, col = c("red", "yellow", "blue", "green", "cyan"), main="PIE CHART 1")
pie(y, names,col = c("red", "yellow", "blue", "green", "cyan"), main="PIE CHART 2")
x <- c(632,20,491,991,20)
y <- c(37376,41770,5210,5005,3947)
names <- c("alpha","beta","gamma","delta","omega")
par(fig=c(0,0.5,0,1))
pie(x, names, col = c("red", "yellow", "blue", "green", "cyan"))
title("CHART 1", line=-3)
par(fig=c(0.5,1,0,1),new=TRUE)
pie(y, names,col = c("red", "yellow", "blue", "green", "cyan"))
title("CHART 2", line=-3)
Alterations:
Par - change control to fig=c(x,x,y,y) to specify that you want each plot to take up a portion of the window so as it is at the moment I have got each pie chart taking up half of the plot window
Par new=TRUE states that you want a second plot "overlaid"
Title - separate from plot, line=x states where you want the title to sit, play around with various - figures until you get what you want
As an alternative, you can also keep using mfrow:
par(mfrow=c(1,2))
pie(x, names, col = c("red", "yellow", "blue", "green", "cyan"))
title("PIE CHART 1", line=-1)
pie(y, names, col = c("red", "yellow", "blue", "green", "cyan"))
title("PIE CHART 2", line=-1)
Related
I have a variable called school_name
I am creating a vector to define colors that I will use later in ggplot2.
colors <- c("School1" = "yellow", "School2" = "red", ______ = "Orange")
In my code I am using the variable school_name for some logic want to add that as the third element of my vector. The value changes in my for loop and cannot be hard-coded.
I have tried the following but it does not work.
colors <- c("School1" = "yellow", "School2" = "red", get("school_name") = "Orange")
Please can someone help me with this
You can use structure:
school_name = "coolSchool"
colors <- structure(c("yellow", "red", "orange"), .Names = c("School1","School2", school_name))
You can just set the names of the colors using names():
colors <- c("yellow", "red", "orange")
names(colors) <- c("School1", "School2", school_name)
This also works:
school_name <- "school3"
colors <- c("School1" = "yellow", "School2" = "red")
colors[school_name] <- "Orange"
# School1 School2 school3
# "yellow" "red" "Orange"
I want to change the default gray color of the bins in a histogram made with gamlss::histDist(). I've tried searching but I can't find any information on how to do it.
Here a small example from the package documentation:
require(gamlss)
data(abdom)
histDist(y,family="NO", ylim=c(0,0.005), data=abdom)
Which produces this plot:
How can I change the color of those bins?
You can not change the bin's color because there is not an available parameter for the user. You can plot by yourself the histogram and then add the density curve.
require(gamlss)
mod <- histDist(y,family="NO", data=abdom)
hist(abdom$y, freq=FALSE, col='pink')
curve(dNO(x, mu=mod$mu, sigma=mod$sigma), col=4, lwd=5, add=TRUE)
Then you can customize the plot.
You may try:
histDist(data=abdom, y,
family="NO",
main="The y and the fitted NO distribution",
xlab = "your choice",
ylab = "Density",
border.hist = "black",
ylim=c(0,0.005),
col.main = "cyan",
col.axis = "brown",
col.lab = "green",
fg.hist = "blue",
line.ty = 1,
line.col = "red",
col.hist = "pink")
getAnywhere(histDist) will show you the function and you may add some extra arguments as cex.main=cex.main, ...
From question Color branches of dendrogram using an existing column, I can color the branches near the leaf of the dendrogram. The code:
x<-1:100
dim(x)<-c(10,10)
set.seed(1)
groups<-c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
x.clust<-as.dendrogram(hclust(dist(x)))
x.clust.dend <- x.clust
labels_colors(x.clust.dend) <- groups
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = groups, edgePar = "col") # add the colors.
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = 3, edgePar = "lwd") # make the lines thick
plot(x.clust.dend)
generates a dendrogram as shown in:
However, I want to color the branches up towards the root until the all the leaves in the current branch have the same labels. Even if there is a single mismatch switch to the default color of black. I want the resulting dendrogram to look like
What I want is little different from using color_branches like
x.clust.dend <-color_branches(x.clust.dend,k=3)
because it colors based on its own clusters not based on some external labels.
The function you are looking for is branches_attr_by_clusters. Here is how to use it:
library(dendextend)
x <- 1:100
dim(x) <- c(10, 10)
set.seed(1)
groups <- c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
dend <- as.dendrogram(hclust(dist(x)))
clusters <- as.numeric(factor(groups, levels = c("red", "blue")))
dend2 <-
branches_attr_by_clusters(dend , clusters, values = groups)
plot(dend2)
This function was originally created to display the results of dynamicTreeCut. See the vignette for another example.
I have tried this
#!/usr/bin/R
fcm <-c(13.0,12.5,11.8)
gk <-c(10.9 , 10.5 , 10.2)
gg <-c(12.0 , 11.0 , 10.8)
data1 <- rbind(fcm,gk,gg)
colnames(data1) <- c(5,6,7)
barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
xlab="number of clusters",ylab="traveltime rms(ms)",
col=c("red", "black", "green"), beside=TRUE)
op <- par(cex=.64)
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"),
"top", horiz=TRUE,text.font=3)
par(op)
dev.copy(png,"s1.png",width=4,height=4,units="in",res=200)
dev.off()
When image appears,R graphics device 2 Active looks fine.But save image has overlapping of words in legend.
How to solve this?
Instead of using dev.copy to save your image, you can call png before "drawing" it :
png("Z:/GES - catalogue signatures/biblio/cdf pour Wolf/s1.png", width=4,height=4,units="in",res=200)
barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
xlab="number of clusters",ylab="traveltime rms(ms)",
col=c("red", "black", "green"), beside=TRUE)
op <- par(cex=.64)
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"),
"top", horiz=TRUE,text.font=3)
par(op)
dev.off()
The following is the example I work on.
require(lattice)
data(barley)
xyplot(yield ~ year | site, data = barley)
I want to put different strip color for different sprips and font color is also different optimized with the backgroud color. For example:
strip background colors = c("black", "green4", "blue", "red", "purple", "yellow")
font color = c("white", "yellow", "white", "white", "green", "red")
Rough sketch of the first one is provided:
How can I achieve this?
Here's a clean and easily customizable solution.
myStripStyle(), the function that is passed in to the strip= argument of xyplot() uses the counter variable which.panel to select colors and also the value of factor.levels for the panel that's currently being plotted.
If you want to play around with the settings, just put a browser() somewhere inside the definition of myStripStyle() and have at it!
bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
txtColors <- c("white", "yellow", "white", "white", "green", "red")
# Create a function to be passed to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, ...) {
panel.rect(0, 0, 1, 1,
col = bgColors[which.panel],
border = 1)
panel.text(x = 0.5, y = 0.5,
font=2,
lab = factor.levels[which.panel],
col = txtColors[which.panel])
}
xyplot(yield ~ year | site, data = barley, strip=myStripStyle)
It might not be wise to refer to variables outside of the scope of the function.
You could use par.strip.text to pass additional arguments to the strip function. par.strip.text can be defined at the plot level and is generally used for setting text display properties, but beeing a list you can use it to bring your variables to the strip function.
bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
txtColors <- c("white", "yellow", "white", "white", "green", "red")
# Create a function to be passes to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, par.strip.text,
custBgCol=par.strip.text$custBgCol,
custTxtCol=par.strip.text$custTxtCol,...) {
panel.rect(0, 0, 1, 1,
col = custBgCol[which.panel],
border = 1)
panel.text(x = 0.5, y = 0.5,
font=2,
lab = factor.levels[which.panel],
col = custTxtCol[which.panel])
}
xyplot(yield ~ year | site, data = barley,
par.strip.text=list(custBgCol=bgColors,
custTxtCol=txtColors),
strip=myStripStyle)