Fairly new to R so sorry if this is a dumb question.
I want to plot a bar chart of a lot of data - maybe 100 bars.
I want to use colours and spacing to highlight the "groups", so I might have the first 10 bars in blue, a small gap, the next 20 in red, a small gap and so on.
I can plot the data fine, but how can I do the colouring and gaps in this way?
This can be done quite easily with ggplot2 as provided in links by #Arun.
With base graphics to set space between bars you can use argument space= (sets space before each bar) and argument col= will change color in function barplot().
Here is a example with 20 bars and space between each 5 bars.
df<-sample(1:10,20,replace=T)
barplot(df,space=c(0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0),
col=rep(c("red","blue","green","yellow"),each=5))
If the number of observations in each group is identical then you can convert vector of values to matrix and then plot it (with argument beside=TRUE). In this case you just need to supply colors but bars will be grouped automatically.
df2<-matrix(df,ncol=4)
barplot(df2,beside=TRUE,col=rep(c("red","blue","green","yellow"),each=5))
Related
I have a curve, for instance
y_curve=c(1,2,5,6,9,1).
and the colors for each curve point
colors=c("#0000FF","#606060","#606060","#FF0000","#FF0000","#FF0000").
In theory I want to plot a curve where the first half has one color (except for the first point which is blue) and the second half has another color. In my example the dataset has more than 3000 observations so it makes sense.
For some reason, if I plot the data just using the command
plot(y_curve,col=colors), the color of points is plotted corrently.
Nevertheless, if I add the option type="l", the plotted curve has only one color - the blue, which is the first color in the vector colors ("#0000FF").
Does anyone know what am I doing wrong?
So the code is
y_curve=c(1,2,5,6,9,1)
colors=c("#0000FF","#606060","#606060","#FF0000","#FF0000","#FF0000")
plot(y_curve,col=colors,type="l")
Thank you all in advance.
I avoid using ggplot since this part of code is inside an already complicated function and I prefer using the base R commands.
The line option for the plot function does not accept multiple colors.
There is the segments() function that we can use to manually draw in each separate segment individually with a unique color.
y_curve=c(1,2,5,6,9,1)
colors=c("#0000FF","#606060","#606060","#FF0000","#FF0000","#FF0000")
#create a mostly blank plot
plot(y_curve,col=NA)
# Use this to show the points:
#plot(y_curve,col=colors)
#index variable
x = seq_along(y_curve)
#draw the segments
segments(head(x,-1), head(y_curve,-1), x[-1], y_curve[-1], type="l", col=colors)
This answer is based on the solution to this question:
How do I plot a graph in R, with the first values in one colour and the next values in another colour?
I need to graph data in ggplot2 for a homework assignment, and while the data is fine, I want the legend for the plot to only have 2 options instead of one for every boxplot. Each of the boxes represents a plot of trees (the x-value) that is either evergreen or deciduous according to the color. However, the fill, which I set to be Forest Type for the legend, made a legend for every single plot when I only wanted two symbols in the legend: the gray45-colred box for the 3 evergreen plots and gray69-colored box for the three deciduous plots. In short, how can I make the plot only have 2 symbols to match the 2 colors?
ggplot(data=dbh.full, aes(x=Plot, y=D_2017,fill=Plot))+geom_boxplot()+
labs(x='Plot ID', y='Tree DBH', fill='Forest Type')+
scale_fill_manual(values=c('gray45','gray45','gray45','gray69','gray69','gray69'))
The professor for this class suggested to use substr() on the dataframe plot labels, so I would prefer to have the problem solved that way, but I am open to simple solutions as well. I placed a link below to a picture of the graph because I am not high enough of a level to post images.
You can create a binary variable and use it as the variable for fill. There many ways to do it, in the example below I detected the levels of Plot that had E to make Evergreen, otherwise Deciduous
library(tidyverse)
dbh %>%
mutate(forest_type = if_else(str_detect(Plot,"E"),"Evergreen","Deciduous")) %>%
ggplot(data=dbh.full, aes(x=Plot, y=D_2017,fill=forest_type))+
geom_boxplot()+
labs(x='Plot ID', y='Tree DBH', fill='Forest Type')+
scale_fill_manual(values=c('gray45','gray69'))
I'm having a problem with the color bar (or color ramp palette) in spplot (adehabitatHR package). I'm want to plot two graphs in the same window using spplot and I also want that the color bar has the same scale for both graphs (just one bar for both graphs). If that is not possible - or too complicated, since I'm new in R language - I wanna set an equal number of tick marks in the bar for both graphs. I had setted this already, but they are in different positions in the color bar - it would make more sense if i have at least one tick mark at lowest value of the bar, one in the middle, and other in the highest valeu. But when I use the colorkey=list(tick.number=3) argument it doesn't allow me to choose where I want the tick marks. How can I do that?
Here is part of my code.
####loading packages....
library("adehabitatHR")
library(latticeExtra)
###... and data
area.total.mari<- read.table("mari.total.txt")
area.total.mari2<- SpatialPoints(area.total.mari)
area.total.mari.mpc=mcp(area.total.mari2)
dry.mari<- read.table("pontos meses seca.txt")
dry.mari1<- SpatialPoints(dry.mari)
dry.mari1.mpc=mcp(dry.mari1)
###color used in the graphs
mycolor2=gray(16:0/16)
##generating kernels and spplot's
ks<-kernelUD(seca.mari1, grid=300, extent=0.2)
ps<-spplot(ks, col.regions=mycolor2,colorkey=list(tick.number=3),
scales=list(draw=T))+
layer(sp.polygons(area.total.mari.mpc))
kc<-kernelUD(chuva.mari1, grid=300, extent=0.35)
pc<-spplot(kc,col.regions=mycolor2,colorkey=list(tick.number=3),
scales=list(draw=T)))+
layer(sp.polygons(area.total.mari.mpc))
###printing graphs
######## Tick marks are messed up :/
print(ps, position=c(0,0,.5,1),more=T)
print(pc, position=c(0.5,0,1,1))
Here is the graph generated with this code:
https://www.flickr.com/photos/129526227#N02/16316924041/
Note the problem with the tick marks: the lowest value ("0") is ok, but I can't place the another value in the highest!
This is an example with the "meuse" data set:
library(sp)
library(lattice)
library(adehabitatHR)
library(latticeExtra)
data(meuse)
coordenadas<-data.frame(meuse$x,meuse$y)
attach(coordenadas)
coord.sp.pt<- SpatialPoints(coordenadas)
coord.sp.pt.mpc=mcp(coord.sp.pt)
mycolor=gray(0:12/12)
coord.sp.ud<-kernelUD(coord.sp.pt, grid=300, extent=0.1)
coord.printing<-spplot(coord.sp.ud, col.regions=colorRampPalette(c("white","gray","black"))
,colorkey=list(tick.number=2),scales=list(draw=T))+
layer(sp.polygons(coord.sp.pt.mpc))
print(coord.printing)
Thanks in advance folkes
I am curious if there's a way to improve upon the answers mentioned in 1
For example,
1) Can the x and y columns of the data-frame be colored differently rather than red or using a color gradient?. And as specified in ggplot2 documentation, I don't want color the columns according to a factor
2) Furthermore, can the shape of points be altered respectively for each of the columns in the data-fame (e.g. triangles for x values and round for y values)
To achieve the same, afaik, I tried to plot each column separately by tweaking the code mentioned in 1
All i got was the same plot with red color for each point with a failure to change the shape when using the aes() function for each column separately.
Thanks and Regards,
Yogesh
Suppose the following R code gives a multiple graph containing four graphs. There are enough spaces among the graphs. How to reduce the space between these graphs? Secondly, How to give axis name to only for the outer side i.e., from the first graph and second graph remove the x axis legend.
getOption("device")()
par(mfrow =c(2,2))
x<-seq(0.01,10,by=0.01)
plot(x,2*x)
plot(x,sin(x))
plot(x,cos(x))
plot(x,x^3)
Try using the margins argument in par(mar). The second plot x-axis is removed using the argument xlab="":
par(mfrow =c(2,2), mar=c(4,4,1,1))
x<-seq(0.01,10,by=0.01)
plot(x,2*x, xlab="") # here the label for the x axis is removed
plot(x,sin(x))
plot(x,cos(x))
plot(x,x^3)