pie(table(games_list$Genre),
main = "Rating Pie Chart",
col=brewer.pal(length(games_list$Genre),'Spectral'))
legend("topright",
legend=row.names(games_list$Genre),
fill = brewer.pal(length(games_list$Rating), 'Spectral'))
Error in legend("topright", legend = row.names(games_list$Genre), fill = brewer.pal(length(games_list$Rating), :
'legend' is of length 0
Check that row.names(games_list$Genre) returns something and is what the legend parameter expects.
Because you did not provide sample data, I am using the iris dataset in the example below:
pie(table(iris$Species),
main = "Rating Pie Chart",
col=brewer.pal(
length(unique(iris$Species)), # we need only unique values
'Spectral'
)
)
legend("topright",
legend=unique(iris$Species),
fill = brewer.pal(
length(unique(iris$Species)),
'Spectral'
)
)
The code above produces:
As an aside, if you have more than 2 or 3 types/classes you want to show, better use barcharts. Here is a good write up by Stephen Few on this topic "Save the Pies for Dessert"
Related
I have created a pie chart and I realize that the title of the pie chart is further away from the pie chart. I want to ask how I can shorten the distance between the title and the chart, using base R.
pie(group3$count, labels = a, col = c("orange", "pink"), main= "The percentage of Fuel type, among Low Emission cluster", cex.main=1 )
legend("bottomright", legend = c("DIESEL/ELECTRIC", "PETROL/ELECTRIC"), fill=c("pink","orange"))
Output:
Many thanks.
You could build the plot separately using legend() and title() and in title call line = 0.6 or any value lower than 1 to move the title closer.
Also I think you are mixing up the colours for either the fill or the legend. one has pink before orange, and the other orange before pink.
automatic <- sum(mtcars$am) / nrow(mtcars)
manual <- 1 - automatic
df <- data.frame(count = c(automatic, manual))
a <- c("Automatic", "Manual")
pie(df$count, col = c("pink", "orange"))
legend(x = "bottomright",
legend = a,
fill = c("pink", "orange"))
title(main = "TITLE", cex.main = 1)
title(main = "TITLE, BUT CLOSER", cex.main = 1, line = 0.6)
Created on 2023-01-09 with reprex v2.0.2
I would like to create a plot of this data, with x-axis increments of 500000 and with sampleIDs on the y-axis. The following code works to create the plot, but the y-axis labels don't work, and I am unsure how to code the x-axis ticks. Also, I had to add headings manually to the data file (and then obviously add header = TRUE when I assigned d) to get the code to work. I shouldn't have had to put the column titles in though should I since I use setNames?
d = read.delim("n_reads_per_sample.tsv", header = TRUE, sep = "\t")
xticks <- ( ? increments of 500000 to xmax ? )
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim = c(0, at = xticks, 1 max(d$n_reads)),
labels = dimnames(d[[1]])
,
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
In case the link doesn't work, this is what the file looks like.
x.sample n_reads
LT-145 3193621
LT-323 786578
LT-458 485543
LT-500 3689123
LT-95 3308764
LT-367 765972
LT-205 2090226
LT-245 10238727
I can't get at your full data right now, so I am just using your sample in the question.
Not sure what you mean that the y-axis labels don't work. They seem OK to me. You can get the x-axis labels that you want by suppressing the x-axis produced by dotchart and then making your own axis using the axis function. That requires a little fancy footwork with par. Also, unless you stretch out your graphics window, there will not be enough room to print all of the axis labels. I reduced the font size and stretched the window to get the graph below.
UpperLimit <- ceiling(max(d$n_reads)/500000)*500000
xticks <- seq(0,UpperLimit, 500000)
par(xaxt = "n")
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim=c(0, UpperLimit),
labels = dimnames(d[[1]]),
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
par(xaxt = "s")
axis(1, at=xticks, cex.axis=0.7)
I am relatively new user in R - and I seem stuck on what should be fairly easy, I am just not finding the problem in my code set-up. I am trying to create a legend on a simply box plot but I cannot get it to line up correctly, without overlaying itself.
My box plot:
boxplot(OS, main='Computer Users Surveyed', xlab='Program Used', ylab= "Seconds (s)", col=c('blue', 'gold1'))
Then when I add a legend:
legend("topright", c("linux", "windows"), border="black", fill = "blue", "gold1")
All it does is show me a blue square with the words gold1 - instead of double stacking the Linux and windows groups with the corresponding colors.
I think you made a simple mistake by not concatenating the fill colors:
Mock data:
OS <- data.frame(
x = rnorm(100),
y = runif(100)
)
boxplot(OS, main='Computer Users Surveyed', xlab='Program Used', ylab= "Seconds (s)", col=c('blue', 'gold1'), frame = F)
legend("topright", c("linux", "windows"), border="black", fill = c("blue", "gold1"))
I'm using R and the lattice package to plot the chart below.
Notice the two items in the legend, that are shown with no spacing between them. If I show the legend above (or below) the chart, it's shown with some space between them, but not if I show in on the right. Is there any way I can separate the two items a bit?
The code used to produce this figure is also reproduced below.
barchart(val1 ~ val2, groups=group, tasks,
auto.key=list(
columns=1,
space="right",
text=c("Data 1","Data 2")
),
main="Title",
xlab="Tasks", ylab="Duration",
par.settings=list(superpose.polygon=list(col=c("firebrick","dodgerblue2")))
)
I think padding.text ist what you are looking for. I dont know, if that works with auto.key.
attach(mtcars)
gear.f<-factor(gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
cyl.f <-factor(cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
densityplot(~mpg|cyl.f,
main="Density Plot by Number of Cylinders",
xlab="Miles per Gallon"
,par.settings = list(superpose.line = list(col=c(1,2,3)))
,key = list(text = list(c("A", "B", "C")),lines = list(col=c("black", "red", "green")),
columns=1,space="top",padding.text=4
)
)
I want to plot 3 barplots together in one graph based on values from different columns of a data frame.
It should look something like this.
The y-values of plot 1 are the sum of the y-values of plot 2 and 3. The color of plot 1 and 2 can be fully filled (e.g. blue & red), but the color of plot 3 has to be translucent.
I was able to make a plot for each column separately using the barplot() function, but I was not able to combine them in one graph.
barplot(covpatient[[1]]$cov, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "blue", col = "blue")
barplot(covpatient[[1]]$plus, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "red", col = "red")
barplot(covpatient[[1]]$min, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "gray", col = "gray")
Could someone give me a hand?
I'm not exactly sure if this is what you want... but based on the graphic that you sent I think this will help:
require(ggplot2)
require(reshape2)
covpatient <-list()
covpatient$cov <-rnorm(100,2)
covpatient$plus <-rnorm(100,4)
covpatient$min <-rnorm(100,1)
plot_covpatient <- do.call(rbind,covpatient)
melted_plot_covpatient<-melt(plot_covpatient,value.name = 'Value')
ggplot(melted_plot_covpatient,aes(group=Var1))+
geom_density(aes(Value,colour=Var1,fill=Var1),alpha=.5)