ggplot2: how to fix the order of the x axis of a geom_bar - axis

I'm new to R. I've produced a bar chart using ggplot2; however, when it plots, the order of the months change along the x axis. I am not sure how to write the code to prevent this from happening and to get the months in the correct order along the x axis.
df <-
data.frame(
month = c(
"May",
"June",
"July",
"August",
"September",
"October",
"November"
),
cpue = c(2.40, 4.20, 6.16, 5.25, 3.32, 2.33, 0.91)
)
ggplot(data = df, aes(x = month, y = cpue)) +
geom_bar(stat = "identity", width =
0.5, fill = "black") + labs(x = "Month", y = "CPUE") + theme(
axis.text = element_text(size = 12),
axis.title = element_text(size = 14),
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank()
)

It is not randomly changing the x axis, but instead it is trying to put the x-axis in an alphabetical order.
An easy fix to this is to make your df ordered using levels.
df <- data.frame(month=c("May","June","July","August","September","October","November"),cpue=c(2.40,4.20,6.16,5.25,3.32,2.33,0.91))
Now that your df is made, we can easily level the column, we will use for the x-axis, in this case the month column:
df$month <- factor(df$month, levels = df$month)
Now, when we plot the bar graph, it is in the order we provided it with from the data frame.
ggplot(data=df,aes(x=month,y=cpue)) +
geom_bar(stat="identity",width=0.5,fill="black") +
labs(x="Month",y="CPUE") +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14),axis.line=element_line(colour="black"),panel.grid.major = element_blank())
Hopefully this helps!

Another option is to use fct_inorder from forcats (part of tidyverse) to keep the original order of the data.
library(tidyverse)
ggplot(data = df, aes(x = fct_inorder(month), y = cpue)) +
geom_bar(stat = "identity", width =
0.5, fill = "black") + labs(x = "Month", y = "CPUE") + theme(
axis.text = element_text(size = 12),
axis.title = element_text(size = 14),
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank()
)
Output

You can also try to use reorder within the ggplot. It allows you to reorder one variable based on another variable, like this:
library(tidyverse)
ggplot(data = df, aes(x = reorder(month, cpue), y = cpue))
Or if you want it in descending order, just add a minus, like this:
ggplot(data = df, aes(x = reorder(month, -cpue), y = cpue))

Related

Calculation of price elasticities with mlogit (mixed logit)

I estimated a mixed logit model with the mlogit package. My data has the following structure:
long format
individual id ("id)
alternative A, B, C D that represent four car types ("alternative")
choice situation ("card")
I estimated a mixed logit with random parameters for price, fuelcost, and range.
Here is my code:
#Read data such that it fits the choice experiment
CAR_P <- mlogit.data(Car_choices, choice = "choice", shape = "long", alt.var = "alternative", chid.var = "card", id.var = "id")
estimation of the mixed logit:
CAR_model_mixed7 <- mlogit(choice ~ price + fuelcost + yearlycost + range + co2 + chargehighway + chargehome | mini + small + large + premium + luxe + age + female + long_distance + ccar + income_fitted + no_charging ,
CAR_P, rpar=c(price ="n", fuelcost = "n", range = "n"), R = 2000, halton = NA, panel = TRUE, reflevel = "A")
summary(CAR_model_mixed7)
Now I need to estimate a price elasticity. But this doesn't work.
A screenshot of my data is added below
The code I entered to estimate the price elasticity is the following:
1. compute a data.frame containing the mean value of the covariates in the sample
z <- with(CAR_P, data.frame(price = tapply(price, idx(CAR_model_mixed7, 2), mean),
fuelcost = tapply(fuelcost, idx(CAR_model_mixed7, 2), mean),
yearlycost = tapply(yearlycost, idx(CAR_model_mixed7, 2), mean),
range = tapply(range, idx(CAR_model_mixed7, 2), mean),
co2 = tapply(co2, idx(CAR_model_mixed7, 2), mean),
chargehighway = tapply(chargehighway, idx(CAR_model_mixed7, 2), mean),
chargehome = tapply(chargehome, idx(CAR_model_mixed7, 2), mean),
mini = mean(mini),
small = mean(small),
large = mean(large),
premium = mean(premium),
luxe = mean(luxe),
age = mean(age),
female = mean(female),
long_distance = mean (long_distance),
ccar = mean(ccar),
income_fitted = mean(income_fitted),
no_charging = mean(no_charging)))
compute the price elasticity:
effects(CAR_model_mixed7, covariate = "price", type = "rr", data = z)
However, I get the error message:
Error in mlogit(formula = choice ~ price + fuelcost + yearlycost + :
no individual index
Does anyone know what I'm doing wrong and how to solve this?

How to loop through the columns of a Matlab table by using the headings?

I am trying to make a for-loop for the Matlab code below. I have named each column with JAN90, FEB90, etc. all the way up to AUG19, which can be found in a matrix named "data". At this point I need to change the month and year manually to obtain the result I want. Is there a way to iterate over the columns by the column name? Would it be easier to name the columns Var1, Var2 etc.?
clear;
clc;
data = readtable('Data.xlsx','ReadVariableNames',false);
data(1,:) = [];
data.Var2 = str2double(data.Var2);
data.Var3 = str2double(data.Var3);
data.Var4 = str2double(data.Var4);
data.Var5 = str2double(data.Var5);
data.Var6 = str2double(data.Var6);
data.Var7 = str2double(data.Var7);
data.Var8 = str2double(data.Var8);
data.Var9 = str2double(data.Var9);
data.Var10 = str2double(data.Var10);
data.Var11 = str2double(data.Var11);
data.Var12 = str2double(data.Var12);
data.Var13 = str2double(data.Var13);
data(:,1) = [];
data = table2array(data);
data = array2table(data.');
data = table2cell(data)
data = cell2table(data, 'VariableNames',{'JAN90','FEB90','MAR90','APR90','MAY90','JUN90','JUL90','AUG90'...
,'SEP90','OCT90','NOV90','DEC90','JAN91','FEB91','MAR91','APR91','MAY91','JUN91','JUL91','AUG91'...
,'SEP91','OCT91','NOV91','DEC91','JAN92','FEB92','MAR92','APR92','MAY92','JUN92','JUL92','AUG92'...
,'SEP92','OCT92','NOV92','DEC92','JAN93','FEB93','MAR93','APR93','MAY93','JUN93','JUL93','AUG93'...
,'SEP93','OCT93','NOV93','DEC93','JAN94','FEB94','MAR94','APR94','MAY94','JUN94','JUL94','AUG94'...
,'SEP94','OCT94','NOV94','DEC94','JAN95','FEB95','MAR95','APR95','MAY95','JUN95','JUL95','AUG95'...
,'SEP95','OCT95','NOV95','DEC95','JAN96','FEB96','MAR96','APR96','MAY96','JUN96','JUL96','AUG96'...
,'SEP96','OCT96','NOV96','DEC96','JAN97','FEB97','MAR97','APR97','MAY97','JUN97','JUL97','AUG97'...
,'SEP97','OCT97','NOV97','DEC97','JAN98','FEB98','MAR98','APR98','MAY98','JUN98','JUL98','AUG98'...
,'SEP98','OCT98','NOV98','DEC98','JAN99','FEB99','MAR99','APR99','MAY99','JUN99','JUL99','AUG99'...
,'SEP99','OCT99','NOV99','DEC99','JAN00','FEB00','MAR00','APR00','MAY00','JUN00','JUL00','AUG00'...
,'SEP00','OCT00','NOV00','DEC00','JAN01','FEB01','MAR01','APR01','MAY01','JUN01','JUL01','AUG01'...
,'SEP01','OCT01','NOV01','DEC01','JAN02','FEB02','MAR02','APR02','MAY02','JUN02','JUL02','AUG02'...
,'SEP02','OCT02','NOV02','DEC02','JAN03','FEB03','MAR03','APR03','MAY03','JUN03','JUL03','AUG03'...
,'SEP03','OCT03','NOV03','DEC03','JAN04','FEB04','MAR04','APR04','MAY04','JUN04','JUL04','AUG04'...
,'SEP04','OCT04','NOV04','DEC04','JAN05','FEB05','MAR05','APR05','MAY05','JUN05','JUL05','AUG05'...
,'SEP05','OCT05','NOV05','DEC05','JAN06','FEB06','MAR06','APR06','MAY06','JUN06','JUL06','AUG06'...
,'SEP06','OCT06','NOV06','DEC06','JAN07','FEB07','MAR07','APR07','MAY07','JUN07','JUL07','AUG07'...
,'SEP07','OCT07','NOV07','DEC07','JAN08','FEB08','MAR08','APR08','MAY08','JUN08','JUL08','AUG08'...
,'SEP08','OCT08','NOV08','DEC08','JAN09','FEB09','MAR09','APR09','MAY09','JUN09','JUL09','AUG09'...
,'SEP09','OCT09','NOV09','DEC09','JAN10','FEB10','MAR10','APR10','MAY10','JUN10','JUL10','AUG10'...
,'SEP10','OCT10','NOV10','DEC10','JAN11','FEB11','MAR11','APR11','MAY11','JUN11','JUL11','AUG11'...
,'SEP11','OCT11','NOV11','DEC11','JAN12','FEB12','MAR12','APR12','MAY12','JUN12','JUL12','AUG12'...
,'SEP12','OCT12','NOV12','DEC12','JAN13','FEB13','MAR13','APR13','MAY13','JUN13','JUL13','AUG13'...
,'SEP13','OCT13','NOV13','DEC13','JAN14','FEB14','MAR14','APR14','MAY14','JUN14','JUL14','AUG14'...
,'SEP14','OCT14','NOV14','DEC14','JAN15','FEB15','MAR15','APR15','MAY15','JUN15','JUL15','AUG15'...
,'SEP15','OCT15','NOV15','DEC15','JAN16','FEB16','MAR16','APR16','MAY16','JUN16','JUL16','AUG16'...
,'SEP16','OCT16','NOV16','DEC16','JAN17','FEB17','MAR17','APR17','MAY17','JUN17','JUL17','AUG17'...
,'SEP17','OCT17','NOV17','DEC17','JAN18','FEB18','MAR18','APR18','MAY18','JUN18','JUL18','AUG18'...
,'SEP18','OCT18','NOV18','DEC18','JAN19','FEB19','MAR19','APR19','MAY19','JUN19','JUL19','AUG19'});
m = [1 2 3 6 12 24 36 60 84 120 240 360]';
for i=1:100
t = i;
data.X_1 = (1-exp(-m./t))./(m./t);
data.X_2 = ((1-exp(-m./t))./(m./t))-exp(-m./t);
model_1 = fitlm(data, 'FEB95 ~ X_1 + X_2');
RSS(100,:) = zeros ;
res = model_1.Residuals.Raw;
res(any(isnan(res), 2), :) = [];
RSS(i) = sum(res.^2);
end
RSS(:,2) = [1:1:100];
min = min(RSS(:,1));
t = find(RSS(:,1) == min)
data.X_1 = (1-exp(-m./t))./(m./t);
data.X_2 = ((1-exp(-m./t))./(m./t))-exp(-m./t);
model_1 = fitlm(data, 'FEB95 ~ X_1 + X_2')
res = model_1.Residuals.Raw;
res(any(isnan(res), 2), :) = [];
RSS = sum(res.^2)
intercept = model_1.Coefficients.Estimate(1,1);
beta_1 = model_1.Coefficients.Estimate(2,1);
beta_2 = model_1.Coefficients.Estimate(3,1);
Yhat = intercept + beta_1.*data.X_1 + beta_2.*data.X_2;
plot(m, Yhat)
hold on
scatter(m, data.FEB95)
I.e "FEB95" should be dynamic? Any suggestions?
Her is how I would approach your problem. First realize that VarNames=data.Properties.VariableNames will get a list of all column names in the table. You could then loop over this list. For example
for v=VarNames
current_column = v{1};
% ....
% Define the model spec for the current column
model_spec = [current_column ' ~ X_1 + X_2'];
% and create the model
model_1 = fitlm(data, model_spec);
% ... Continue computation ... and collect results in a table or array
end

Local Binary Patterns features

I have a dataset of images that differ in sizes, and when I extract the local binary pattern (uniform local binary 59 feature) from them I get several features as zeros, is it acceptable as features, if not, and how to deal with it
You can use this solution: https://github.com/arsho/local_binary_patterns
def get_pixel(img, center, x, y):
new_value = 0
try:
if img[x][y] >= center:
new_value = 1
except:
pass
return new_value
def lbp_calculated_pixel(img, x, y):
center = img[x][y]
val_ar = []
val_ar.append(get_pixel(img, center, x-1, y+1)) # top_right
val_ar.append(get_pixel(img, center, x, y+1)) # right
val_ar.append(get_pixel(img, center, x+1, y+1)) # bottom_right
val_ar.append(get_pixel(img, center, x+1, y)) # bottom
val_ar.append(get_pixel(img, center, x+1, y-1)) # bottom_left
val_ar.append(get_pixel(img, center, x, y-1)) # left
val_ar.append(get_pixel(img, center, x-1, y-1)) # top_left
val_ar.append(get_pixel(img, center, x-1, y)) # top
power_val = [1, 2, 4, 8, 16, 32, 64, 128]
val = 0
for i in range(len(val_ar)):
val += val_ar[i] * power_val[i]
return val
# Function to generate horizontal projection profile
def getHorizontalProjectionProfile(image):
# Convert black spots to ones
image[image == 0] = 1
# Convert white spots to zeros
image[image == 255] = 0
horizontal_projection = np.sum(image, axis = 1)
return horizontal_projection
# Function to generate vertical projection profile
def getVerticalProjectionProfile(image):
# Convert black spots to ones
image[image == 0] = 1
# Convert white spots to zeros
image[image == 255] = 0
vertical_projection = np.sum(image, axis = 0)
return vertical_projection
lenx = x_train.shape[0]
x_train_lbp = np.zeros((lenx, 32,32))
for NUM in range(lenx):
gray = rgb2gray(x_train[NUM])
for i in range(0, 32):
for j in range(0, 32):
x_train_lbp[NUM][i][j] = lbp_calculated_pixel(gray, i, j)
lenx = x_test.shape[0]
x_test_lbp = np.zeros((lenx, 32,32))
for NUM in range(lenx):
gray = rgb2gray(x_test[NUM])
for i in range(0, 32):
for j in range(0, 32):
x_test_lbp[NUM][i][j] = lbp_calculated_pixel(gray, i, j)
from sklearn import preprocessing
lenx = x_train.shape[0]
x_train_lbp_vector = np.zeros((lenx, 64))
for NUM in range(lenx):
horizontal_projection = getHorizontalProjectionProfile(x_train_lbp[NUM])
vertical_projection = getVerticalProjectionProfile(x_train_lbp[NUM])
x_train_lbp_vector[NUM] = np.concatenate((horizontal_projection, vertical_projection), axis=0)
x_train_lbp_vector[NUM] = normalize(x_train_lbp_vector[NUM].reshape(1, -1))
lenx = x_test.shape[0]
x_test_lbp_vector = np.zeros((lenx, 64))
for NUM in range(lenx):
horizontal_projection = getHorizontalProjectionProfile(x_test_lbp[NUM])
vertical_projection = getVerticalProjectionProfile(x_test_lbp[NUM])
x_test_lbp_vector[NUM] = np.concatenate((horizontal_projection, vertical_projection), axis=0)
x_test_lbp_vector[NUM] = normalize(x_test_lbp_vector[NUM].reshape(1, -1))

Labeling x axis on qicharts

I have produced the Individuals control chart depicted below. The data set has a variable named date that is in R format YMD. I would like to have the x axis display each YMD with a label identifying the specific YMD.
Here is my current code to produce the individuals control chart: qic(data$records, chart=c("i"), x.format="%Y-%m-%d").
Individuals Control Chart
Thanks!
You need to specify the x and xlab arguments. Here's an example using the updated qicharts2 package:
library(qicharts2)
# Build data frame for example
df <- data.frame(x = rep(1:24, 4),
ReportMonth = (rep(seq(as.Date('2014-1-1'),
length.out = 24,
by = 'month'),
4)),
num = rbinom(4 * 24, 100, 0.5),
denom = round(runif(4 * 24, 90, 110)),
grp1 = rep(c('g', 'h'), each = 48),
grp2 = rep(c('A', 'B'), each = 24))
#now plot an 'i' chart :
qic(
x= ReportMonth,
y= num,
# n= denom,
data=df,
chart= "i",
x.format="%Y-%m-%d",
x.angle = 90,
y.expand = 40, # where to start y axis from
xlab = "Month",
ylab= "Value")
Output:

Move y axis labels to left side of heatmap.2

I would like to move the y-axis labels to the left side of heatmap.2. (This is similar, but not the same, as the question regarding moving the axis on heatmap)
While I am able to move the axis by editing line 290 of the heatmap.2 function, it the values then overwrite the actual heatmap.
if (is.null(srtRow) && is.null(colRow)) {
axis(4, iy, labels = labRow, las = 2, line = -0.5 + offsetRow,
tick = 0, cex.axis = cexRow, hadj = adjRow[1], padj = adjRow[2])
}
else {
if (is.null(srtRow) || is.numeric(srtRow)) {
xpd.orig <- par("xpd")
par(xpd = NA)
ypos <- axis(4, iy, labels = rep("", nr), las = 2, #change
line = -0.5, tick = 0)
text(x = par("usr")[2] + (1 + offsetRow) * strwidth("M"),
y = ypos, labels = labRow, adj = adjRow, cex = cexRow,
srt = srtRow, col = colRow)
par(xpd = xpd.orig)
}
I tried moving the location of the heatmap.2 by mucking about with the lwid and lhei options, but the overwrite problem persisted.
Thank you
Eric