Using lapply on a dataframe to create histograms with labels - r

I have created some code to generate histograms using apply on the state.x77 dataset.
attach(as.data.frame(state.x77))
lapply(X=c("Population","Income","Illiteracy","Life Exp","Murder","HS Grad","Frost","Area"),FUN=function(s)hist(state.x77[,s],main=paste("Hist of",s)))
It works fine with generating the titles for the graphs but I do not know how to create labels for the x and y-axis.
The y-axis has the label "frequency" which is fine but I want to make it so that each x-axis is labelled according to the corresponding variable in state.x77.
How would I generate the labels?

I found the answer
lapply(X=c("Population","Income","Illiteracy","Life Exp","Murder","HS Grad","Frost","Area"),FUN=function(s)hist(state.x77[,s],main=paste("Hist of",s),xlab=s))
Adding xlab=s works for the labels.

Related

viewing rownames in a heatmap in R

My aim is creating a heatmap with a continuous legend. I tried a few paths and ended up with pheatmap. Legend is great, names of columns are ok, but the names of the rows don't show. I'd also like plot as squares not rectagles. Any ideas?

Julia - Displaying several plots in the same plot (not subplot)

Plotting several series in a same plot display is possible and also several subplots in a display. But I want several plots which can be completely different things (not necessarily a series or graph of a map) to be displayed exactly in one frame. How can I do that? In Maple you assign names for each plot like
P1:=...:, P2:= ...: and then using plots:-display(P1,P2,...); and it works. But I want to do this in Julia. Let's say I have the following plots as an example;
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
plot(x,y)
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :yellow))
Now how to have both P1 and P2 in one plot? I don't one a shortcut or trick to write the output of this specific example with one plot line, note that my question is general, for example p2 can be a curve or something else, or I may have a forflow which generates a plot in each step and then I want to put all those shapes in one plot display at the end of the for loop.
Code for a simple example of trying to use plot!() for adding to a plot with arbitrary order.
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot!(x2,y2,fill=(0, :orange))
p3=plot(x,y)
display(p2)
p5=plot!([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green))
By running the above code I see the following plots respectively.
But what I expected to see is a plot with the green rectangle added inside the plot with the two orange rectangles.
The way to plot several series within the same set of axes is with the plot! function. Note the exclamation mark! It's part of the function name. While plot creates a new plot each time it is invoked, plot! will add the series to the current plot. Example:
plot(x, y)
plot!(x, z)
And if you are creating several plots at once, you can name them and refer to them in plot!:
p1 = plot(x, y)
plot!(p1, x, z)
Well, if you do that, what you will have is subplots, technically. That's what it means.
The syntax is
plot(p1, p2)
Sorry, I don't know how to plot a whole plot (conversely to a series) over an other plot.. For what it concerns the order of the plots, you can create as many plots as you want without display them and then display them wherever you want, e.g.:
using Plots
pyplot()
# Here we create independent plots, without displaying them:
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange));
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :orange));
p3=plot(x,y);
p5=plot([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green));
# Here we display the plots (in the order we want):
println("P2:")
display(p2)
println("P3:")
display(p3)
println("P5:")
display(p5)
println("P1:")
display(p1)

ggplot2: Can I add the strip labels to every plot with facet_grid()?

Here is a typical case for using multiple variables in the rows or columns in facet_grid().
My question is how can I add the strip labels to every plot? For instance, the second row only contains the strip label "f" in the y-axis. I need all six plots all get two strip labels in X and Y.
I am new to ggplot2 and R, I hope you can help me. Thank you all so much.

R plot() - why do my points appear as horizontal lines

I'm trying to make a plot in R. My x-axis is a week number converted to factor and my y-axis is an amount.
When I run plot() instead of dots I get horizontal lines.
Why does this happen?
Here is a sample dataset:
df <- data.frame(fin_week=as.factor(seq(1,20, by =1)), amount=(rnorm(20)^2)*100)
plot(df)
Looking at the documentation, it's because the first column is a factor. When R tries to find the right plot() to run, it looks into plot.dataframe, where it plots on the type of 1st column i.e a factor. Hence it plots using plot.factor(), which gives a line by default, which is used for box plots.
try using plot.default(df) to plot and you should get it the scatter plot

Multiple beside barplots with different variables in R

I have three lists in R like these;
> list1<-list(Joseph=12, Johan=10, Dave=15,Steve=3,Jo=8)
> list2<-list(Joseph=12, David=10,George=2,Damian=20,Louis=2)
> list3<-list(Bill=22,David=2,Peter=2,Dave=18,Sebastian=8,William=3)
each column has a name label and a numeric score.
I want to display 3 barcharts, one beside the other; each barchart plots the 3 main scored names for each list, saving the label.
For instance, the first barplot shows Dave,Joseph,Johan with heights 15,12,10. The second barplot shows Damian,Joseph and David with heights 20,12 and 10, while the third barplot shows Bill,Dave,Sebastian with heights 22,18 and 8.
I found only examples where the very same variables are plotted in multiple barplots one beside the other in different experiments, but here nominally variables might keep changing from a barplot to another.
How to achieve my goal?
Using multiplot function given in the link. I am also using ggplot2 and reshape2 :
p1<-ggplot(melt(data.frame(list1)),aes(x=variable,y=value))+geom_bar(stat='identity')
p2<-ggplot(melt(data.frame(list2)),aes(x=variable,y=value))+geom_bar(stat='identity')
p3<-ggplot(melt(data.frame(list3)),aes(x=variable,y=value))+geom_bar(stat='identity')
multiplot(p1, p2, p3, cols=1)
Another option is to use grid.arrange() in the gridExtra package:
grid.arrange(p1, p2,p3,ncol=3)
You can use par and mfcol/mfrow for this:
par(mfcol=c(1,3))
barplot(unlist(list1))
barplot(unlist(list2))
barplot(unlist(list3))
par(mfcol=c(1,1))

Resources