I have data that falls into one of many categories. Here is a very simplified version of what I'm doing to my data. I want to make a scatterplot where different colors represent different categories. However, there are many different categories so instead of manually choosing the colors, I'm letting R choose for me by setting col=data$category within the plot function. However, I can't figure out how to generate a legend -- every parameter that I put inside the legend function never actually generates anything. Can someone help?
data <- data.frame(rnorm(50),sample(1:10,50,replace=TRUE))
colnames(data) <- c("data", "category")
plot(data$data, col=data$category)
legend("topright", data$category)
Try something like this,
legend("topright", legend=unique(data$category), pch=1, col=unique(data$category))
Related
I have a large dataset and I am trying to create a stacked bar chart that contains countries and years. Here is what I have right now:
counts <- table(IMDB$country, IMDB$year)
barplot(counts, main="Movies produced in each country by year",
xlab="Years", col=colorRampPalette(brewer.pal(name="Dark2", n = 8))(15),
legend = rownames(counts))
How can I make all the countries visible in the legend because in my case not all of them appear, moreover, the colours look too similar so I can't distinguish sometimes between them?
You need to set a custom length and width parameter before starting your plot. See
?png() or the related pdf(), svg(), etc.
Your best palette choices are those with a sufficient spread across the color wheel to allow easy discrimination.
Added: Re. your example request, there are many already here and in the examples using "?". However...
png(filename= "rplotpng.png",width=1000, height=400, units ="px")
plot(1:30,rep(2,30))
dev.off()
# you will find the output in your working directory.
I'm really new to R and i'm trying to group the x axis together instead it being separate like it it and also move the legend.
Graph and Code http://127.0.0.1:41763/graphics/plot_zoom_png?width=1200&height=455
Ran<-table(data$class, data$feeling)
Raw<-barplot(Ran, main="Class Feeling",xlab="Feeling", col=c("darkblue","red"), legend = rownames(Ran), beside=TRUE)
I would help with moving the legend and have the x-axis grouped as one. on the group its two separate, like Great and Great, where i just want one great on the group with the data together by the different times
You can specify legend position and others by using args.legend(). Unfortunately I can't tell you exactly what to do without knowing what data set would looks. However, I think this page might help you.
I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)
I have a dataset as follows:
(10,75)
(20,80)
(50,85)
(100,92)
How to plot a bar-graph in R? I saw many examples in the net but none of them conform to this simple circumstance. Thanks
Try this:
data1=rbind(c(10,20,50,100),c(75,80,85,92))
barplot(data1, beside=TRUE, col=c("blue", "red"))
As an alternative, you can always use the ggplot2 library. Because of the way the data is shaped, you should also use the reshape2 library to differentiate between variables. It's a bit more complicated in this case, but in general you'll get nicer-looking barplots.
library(ggplot2)
library(reshape2)
#id variable tells what row number is used
data1=as.data.frame(cbind(id=1:4,var1=c(10,20,50,100),var2=c(75,80,85,92)))
#melt will create a row for each variable of each row, except it saves the id as a separate variable that's on every row
data1=melt(data1,id.vars='id')
#ggplot tells what data set is used and which variables do what
#geom_bar tells what type of plot should be used and certain options for the plot
ggplot(data1,aes(x=id,y=value,fill=variable))+geom_bar(stat='identity',position='dodge')
I just wanted how know what category matches with everypoint when I do this:
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group)
I know how to make a legend and put text with an arbitrary vector c('A','B',C'), but is there a more "automatic" way for doing this? This is an easy example but I need to do it with residuals or survival functions plot
Thank you in advance.
The traditional graphics system provides the legend function for adding a
legend or key to a plot. But It should be noted that it is entirely the responsibility of the user to ensure that the legend corresponds to the plot. There is no automatic checking that
data symbols in the legend match those in the plot. It is simpler to do it using lattice or ggplot2. for example:
library(lattice)
xyplot(y~x,groups=group,auto.key=T)
if you want absolutly to use base graphics, you can do this :
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group,pch=as.numeric(group))
legend(2, 500, c('A','B','C'),
cex=1.5, pch=1:3,col=1:3)