Alternatives to the barplot function - r

I have a doubt about the use of the barplot function, I have the following function that receives a data.frame as parameter, which can vary widely in the number of rows. I want to print a histogram as image or likeness. The problem is that I always have problems barplot margins. Is there any way to do the same histogram with another library that no problems margins?
function:
HIST_EPC_list<-function(DF_TAG_PHASE_EPC_counter){
num<-nrow(DF_TAG_PHASE_EPC_counter)
barplot(DF_TAG_PHASE_EPC_counter$Num_EPC, names.arg = DF_TAG_PHASE_EPC_counter$Tag_PHASE, xlab = "Tag_PHASE", ylab = "Num_EPC", main="Histograma Num tags/PHASE:", width=40)
par(mar=c(10,10,10,10))
}
data.frame example:
DF_TAG_PHASE_EPC_counter
Tag_PHASE Num_EPC
1 123.0 1
2 75.0 1
3 78.0 1
4 81.0 2
5 84.0 1
6 87.0 1
7 90.0 2
8 98.0 1
Error:
Error in plot.new() : figure margins too large
Called from: barplot(DF_TAG_RSSI_EPC_counter$Num_EPC, names.arg = DF_TAG_RSSI_EPC_counter$Tag_RSSI,
xlab = "Tag_RSSI", ylab = "Num_EPC", main = "Histograma Num tags/RSSI:",
width = 10)

Related

Visualising the distribution for different subgroups

I'm using "d.pizza" data. There is variable called "delivery_min" which is delivery time (in minutes) and there is variable called "area" which can be one of three areas (Camden, Westminster and Brent).
I want to draw a density plot that visualises the distribution of delivery time for these three areas.
I tried
plot.ecdf(pizza_d$delivery_min)
this code works, but how can I do it for each area?
head(d.pizza)=
index date week weekday area count rabate price operator driver delivery_min
1 1 1 01.03.2014 9 6 Camden 5 TRUE 65.655 Rhonda Taylor 20.0
2 2 2 01.03.2014 9 6 Westminster 2 FALSE 26.980 Rhonda Butcher 19.6
3 3 3 01.03.2014 9 6 Westminster 3 FALSE 40.970 Allanah Butcher 17.8
4 4 4 01.03.2014 9 6 Brent 2 FALSE 25.980 Allanah Taylor 37.3
5 5 5 01.03.2014 9 6 Brent 5 TRUE 57.555 Rhonda Carter 21.8
6 6 6 01.03.2014 9 6 Camden 1 FALSE 13.990 Allanah Taylor 48.7
temperature wine_ordered wine_delivered wrongpizza quality
1 53.0 0 0 FALSE medium
2 56.4 0 0 FALSE high
3 36.5 0 0 FALSE <NA>
4 NA 0 0 FALSE <NA>
5 50.0 0 0 FALSE medium
6 27.0 0 0 FALSE low
You could do:
library(DescTools)
data(d.pizza)
plot.ecdf(subset(d.pizza, area == "Camden")$delivery_min,
col = "red", main = "ECDF for pizza deliveries")
plot.ecdf(subset(d.pizza, area == "Westminster")$delivery_min,
add = TRUE, col = "blue")
plot.ecdf(subset(d.pizza, area == "Brent")$delivery_min,
add = TRUE, col = "green")
library(DescTools)
data(d.pizza)
summary(d.pizza$delivery_min)
plot(NULL,ylab='',xlab='', xlim=c(5,66), ylim=0:1)
for(A in 1:3) {
plot.ecdf(d.pizza$delivery_min[d.pizza$area == levels(d.pizza$area)[A]],
pch=20, col=A+1, add=T)
}
legend("bottomright", legend=levels(d.pizza$area),
bty='n', pch=20, col=2:4)
I'd recommend the ggplot2 library for data visualization in R. Here's some code using ggplot2 that can create a density plot with the three groups overlaid:
library(ggplot2)
# make example dataframe
d.pizza <- data.frame(delivery_min = rnorm(n=30), area = rep(c("Camden", "Westminster", "Brent"), 10))
# plot data in ggplot2
ggplot(d.pizza, aes(x = delivery_min, fill = area, color = area)) + geom_density(alpha = 0.5)
If you want a histogram, that can be done too:
ggplot(d.pizza, aes(x = delivery_min, fill = area, color = area)) + geom_histogram(alpha = 0.5, position = 'identity')

Labels missing in barplot

I have a dataset that I would like to visualize with barplot() . My question is, why do some labels not show when appended with text() and how does one solve this issue?
For example this is my table
table(test$Freq)
2 3 4 5 6 7 8 9 10 11 12 14 16 44
6338 2544 1072 394 102 29 11 9 5 2 3 1 1 1
And the following barplot will miss the first label:
barplot(table(test$Freq))
text(x = xx, y = test$Freq, label = test$Freq, pos = 3, cex = 0.8, col = "red")
It looks like the text is being plotted outside of your graph.
Try adjusting the ylim value when you call barplot. This should solve your problem.

text not showing up on plot in r

I have a dataframe named tab of the following format:
Var1 Freq
1 5 1853
2 15 2862
3 25 7206
4 35 14890
5 45 19856
6 55 23837
7 65 16510
8 75 4729
9 85 830
I want to make a barplot and have the Freq displayed on the bar.
I have tried the following:
plot(tab$Var1, tab$Freq)
text(
x = tab$Var1,
y = tab$Freq,
labels = row.names( tab$Freq ),
adj = 0)
There seems to be two things wrong here:
The plot is showing lines instead of bars, and barplot(tab$Freq)
axis(1, xaxp = tab$Var1) gives error that Error in axis(1, xaxp = tab$Var1) : plot.new has not been called yet (PART 1 SOLVED).
Using the original plot and text function, the texts do not show up.
Thanks.

Plotting tetrahedron with data points in R

I'm in a little bit of pain at the moment.
I'm looking for a way to plot compositional data.(https://en.wikipedia.org/wiki/Compositional_data). I have four categories so data must be representable in a 3d simplex ( since one category is always 1 minus the sum of others).
So I have to plot a tetrahedron (edges will be my four categories) that contains my data points.
I've found this github https://gist.github.com/rmaia/5439815 but the use of pavo package(tcs, vismodel...) is pretty obscure to me.
I've also found something else in composition package, with function plot3D. But in this case an RGL device is open(?!) and I don't really need a rotating plot but just a static plot, since I want to save as an image and insert into my thesis.
Update: data looks like this. Consider only columns violent_crime (total), rape, murder, robbery, aggravated_assault
[ cities violent_crime murder rape rape(legally revised) robbery
1 Autauga 68 2 8 NA 6
2 Baldwin 98 0 4 NA 18
3 Barbour 17 2 2 NA 2
4 Bibb 4 0 1 NA 0
5 Blount 90 0 6 NA 1
6 Bullock 15 0 0 NA 3
7 Butler 44 1 7 NA 4
8 Calhoun 15 0 3 NA 1
9 Chambers 4 0 0 NA 2
10 Cherokee 49 2 8 NA 2
aggravated_assault
1 52
2 76
3 11
4 3
5 83
6 12
7 32
8 11
9 2
10 37
Update: my final plot with composition package
Here is how you can do this without a dedicated package by using geometry and plot3D. Using the data you provided:
# Load test data
df <- read.csv("test.csv")[, c("murder", "robbery", "rape", "aggravated_assault")]
# Convert absolute data to relative
df <- t(apply(df, 1, function(x) x / sum(x)))
# Compute tetrahedron coordinates according to https://mathoverflow.net/a/184585
simplex <- function(n) {
qr.Q(qr(matrix(1, nrow=n)) ,complete = TRUE)[,-1]
}
tetra <- simplex(4)
# Convert barycentric coordinates (4D) to cartesian coordinates (3D)
library(geometry)
df3D <- bary2cart(tetra, df)
# Plot data
library(plot3D)
scatter3D(df3D[,1], df3D[,2], df3D[,3],
xlim = range(tetra[,1]), ylim = range(tetra[,2]), zlim = range(tetra[,3]),
col = "blue", pch = 16, box = FALSE, theta = 120)
lines3D(tetra[c(1,2,3,4,1,3,1,2,4),1],
tetra[c(1,2,3,4,1,3,1,2,4),2],
tetra[c(1,2,3,4,1,3,1,2,4),3],
col = "grey", add = TRUE)
text3D(tetra[,1], tetra[,2], tetra[,3],
colnames(df), add = TRUE)
You can tweak the orientation with the phi and theta arguments in scatter3D.

Plotting a data frame in R

I have this data frame and I'd like to know if there's a way to plot this using the ggplot2 library (or anything that works). The first row has a bunch of zip codes and the second row contains weather data (temperature in this case) associated with the corresponding zip code. I want to create a graph (bar/plot) with the zip codes on the x axis and the temperature values on the y axis but I don't know how to do it.
V1 V2
1 20904 82.9
2 20905 80.1
3 20906 84.6
4 20907 84.6
5 20908 88.0
6 20910 84.6
7 20911 84.6
8 20912 86.1
9 20913 86.1
10 20914 80.7
11 20915 84.6
You also can do a simple barplot:
ydf <- ZipGraph
barplot(ydf[2,],names.arg = ydf[1,],col=rainbow(ncol(ydf)),
xlab="zipcode",ylab="Temperature",cex.axis = .8,cex.names = .7)
Edit: Or you can do
ylims=c(0,max(ydf[,2])*1.2)
y1=barplot(ydf[,2],col=rainbow(nrow(ydf)),xaxt="n",ylim=ylims,
xlab="zipcode",ylab="Temperature",cex.axis = .8)
axis(1, at=y1,labels=ydf[,1],las=2,cex.axis= .6)

Resources