How to add ellipse in bray nmds analysis in vegan package - r

I have plotted point graph using vegan package but I want to circle the similarly treated species. As shown in the figure, 3 colors for 3 treatments. I want to circle them too.
Here is my code.
library(vegan)
library(MASS)
library(readxl)
bray1 <- read_excel("bray1.xlsx")
cols <- c("red", "blue","blue", "green","green","red","blue","green","green","red","red","blue")
row.names(bray1) <- c("SI1", "SII0", "SI0", "SII2", "SI2", "SII1", "SIII0", "SIV2", "SIII2", "SIV1", "SIII1", "SIV0")
bcdist <- vegdist(bray1, "bray")
bcmds <- isoMDS(bcdist, k = 2)
plot(bcmds$points, type = "n", xlab = "", ylab = "")
text(bcmds$points, dimnames(bray1)[[1]],col = cols,size=10)
[My data
bray1<-structure(list(`Andropogon virginicus` = c(0, 0, 0, 0, 2.7, 31.5333333333333, 0, 0, 0, 0, 0, 0), `Oenothera parviflora` = c(61.6,30.3333333333333, 7.53333333333333, 0, 11.7333333333333, 0, 0, 0,75.4, 0, 0, 0), `Lespedeza cuneata` = c(0, 0, 0, 0, 0, 46.7333333333333, 0, 0, 3, 0, 0, 0), `Lespedeza pilosa` = c(0, 1.93333333333333, 0, 0, 1.73333333333333, 0, 0, 0, 0, 1.7, 0, 0), `Chamaesyce maculata` = c(0, 0, 0,4.733333333, 0, 0, 0, 0, 0, 0, 0, 0), `Chamaesyce nutans` = c(0,0, 0, 0, 0,0, 0.166666666666667, 0, 0, 0, 0, 0), `Bidens frondosa` = c(0, 0, 0,1.76666666666667, 1.03333333333333, 3.23333333333333, 0, 0, 0, 0, 0, 0), `Erigeron annuus` = c(0, 0, 0, 0, 0.4, 0, 0, 0, 0, 0, 0, 0), `Erigeron canadensis` = c(0, 0, 0, 0, 0, 4.33333333333333, 0, 0, 9.1, 2.066666667, 0,0), `Equisetum arvense` = c(46, 62.7333333333333, 0, 1.66666666666667, 0, 0.533333333333333, 0, 0, 0, 0, 0, 0), `Erigeron sumatrensis` = c(0, 0, 0, 0, 0, 16.4333333333333, 0, 4, 0, 6.633333333, 0, 0), `Hypochaeris radicata` = c(0, 3.76666666666667, 116.6, 0, 5.033333333, 9.76666666666667, 29, 0, 23.1666666666667, 82.16666667, 0, 0), `Lactuca indica` = c(10.26666667, 0, 1.566666667, 120.1333333, 44.36666667, 42.0333333333333, 0, 14.2333333333333, 0, 0, 14.36666667, 22.2), `Solidago altissima` = c(0, 1.06666666666667, 33.93333333, 0, 0, 0, 0, 0, 0, 6.6, 0, 0), `Sonchus asper` = c(0, 35.9, 0, 0, 0, 7.46666666666667,
29.6666666666667, 4.96666666666667, 0, 0, 0.23, 2.933333333 )), .Names = c("Andropogon virginicus", "Oenothera parviflora", "Lespedeza cuneata", "Lespedeza pilosa", "Chamaesyce maculata", "Chamaesyce nutans", "Bidens frondosa", "Erigeron annuus", "Erigeron canadensis", "Equisetum arvense", "Erigeron sumatrensis", "Hypochaeris radicata", "Lactuca indica", "Solidago altissima", "Sonchus asper"), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))

Here are a couple of alternatives based on the dataEllipse function in the car package. I have made a few minor alterations to your base graph. I found it hard to read the pure "green" color text, so I switched it to "darkgreen". I changed the plotting limits so that the full ellipses would be in the picture. Also, your text statement included an argument size. text does not have an argument size so I replaced it with cex to set the font size.
library(car)
Group = c(1,2,2,3,3,1,2,3,3,1,1,2)
cols <- c("red", "blue","blue", "darkgreen","darkgreen","red","blue",
"darkgreen","darkgreen","red","red","blue")
In the first version, I did what I think you asked for, ellipses marking the treatment groups.
plot(bcmds$points, type = "n", xlab = "", ylab = "",
xlim=c(-0.8,0.8), ylim=c(-0.8,0.8), asp=1)
text(bcmds$points, dimnames(bray1)[[1]],col = cols, cex=0.8)
dataEllipse(bcmds$points[,1], bcmds$points[,2], factor(Group),
plot.points=F, add=T, col=c("red", "blue", "green"),
levels=rep(0.6, 3), center.pch=0, group.labels="", lwd=1)
In the second version, instead of using the outline of the ellipse, I use a transparent fill color to show the ellipses.
plot(bcmds$points, type = "n", xlab = "", ylab = "",
xlim=c(-0.8,0.8), ylim=c(-0.8,0.8), asp=1)
text(bcmds$points, dimnames(bray1)[[1]],col = cols, cex=0.8)
dataEllipse(bcmds$points[,1], bcmds$points[,2], factor(Group),
plot.points=F, add=T, col=c("red", "blue", "green"),
levels=rep(0.6, 3), center.pch=0, group.labels="",
lty=0, fill=TRUE, fill.alpha=0.04)

Related

Aggregate similar constructs/ FA with binary variables

I would like to aggregate, in order to reduce the number of constructs, its following data frame containing only binary variables that correspond to "yes/no", its following data frame (first 10 row). The original data frame contains 169 rows.
outcome <-
structure(list(Q9_Automazione.processi = c(0, 0, 0, 0, 0, 0,
1, 1, 1, 0), Q9_Velocita.Prod = c(1, 0, 0, 1, 0, 0, 1, 1, 1,
0), Q9_Flessibilita.Prod = c(0, 0, 0, 1, 0, 0, 1, 1, 0, 1), Q9_Controllo.processi = c(0,
0, 0, 1, 0, 0, 1, 1, 0, 0), Q9_Effic.Magazzino = c(0, 0, 0, 1,
0, 0, 0, 0, 0, 0), Q9_Riduz.Costi = c(0, 1, 0, 0, 0, 0, 0, 0,
0, 1), Q9_Miglior.Sicurezza = c(0, 0, 0, 0, 0, 0, 1, 0, 1, 1),
Q9_Connett.Interna = c(0, 0, 0, 0, 0, 0, 0, 1, 1, 0), Q9_Connett.Esterna = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Virtualizzazione = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0), Q9_Innov.Prod = c(0, 0, 0, 0, 0,
1, 0, 0, 0, 1), Q9_Person.Prod = c(0, 1, 0, 1, 0, 1, 0, 0,
0, 1), Q9_Nuovi.Mercati = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Q9_Nuovi.BM = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Perform.Energ = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Perform.SostAmb = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame")
I have tried performing factor analysis via the tethracoric method on the obtained correlation matrix ( the obtained value from the KMO function turns out to be inadequate) both directly on the dataframe and then using tethracoric correletions in fafunction (using cor = "tet" I get a negative Tucker Lewis Index).
I have been reading up on this but cannot find a methodology that is adequate and of which I am certain of the correctness of the analysis.
So basically what I would like to achieve is to aggregate similar constructs, e.g., assess whether column 5 has value 1 (i.e., "yes") almost always when column 11 has value 1 and then aggregate.
Here the code that I try to used
library(psych)
tet <- tetrachoric(outcome)
corrplot(tet$rho, "ellipse", tl.cex = 0.75, tl.col = "black")
par(mfrow = c(1,2))
corr_matrix %>%
ggcorrplot(show.diag = F,
type="lower",
lab=TRUE,
lab_size=2)
KMO(corr_matrix)
cortest.bartlett(corr_matrix)
fa.parallel(corr_matrix, fm = "ml")
factor <- fa(corr_matrix, nfactors = 3, rotate = "oblimin", fm = "ml")
print(factor, cut = 0.3, digits = 3)
# -------- Pearson --------
cor(outcome, method = 'pearson', use = "pairwise.complete.obs") %>%
ggcorrplot(show.diag = F,
type="lower",
lab=TRUE,
lab_size=2)
KMO(outcome)
cortest.bartlett(outcome)
fa.parallel(outcome)
factor1 <- fa(outcome, nfactors = 3, rotate = "oblimin", cor = "tet", fm = "ml")
print(factor1, cut = 0.3, digits = 3)

Multiple Calibration plots with val.prob.ci.2

I am trying to create two calibration plots (for two different models) but it does not seem to work. My data (a subset of them):
structure(list(X1 = c(0.0205881308065423, 0.030107400545467,
0.0224902821967529, 0.067082269138019, 0.0128933436225658, 0.010528298470225,
0.0448801718109416, 0.0147825838164296, 0.00127338570492985,
0.0187288824619526, 0.0166935353708351, 0.000827013756910522,
0.000268624600100464, 0.00022554771787564, 0.000239290116892055,
0.00046320712675918, 0.0127930773405932, 0.123559021969098, 0.00196413334593659,
0.00267343502355055, 0.0119560304531064, 0.0151288958940289,
0.0450932732709064, 0.284128554073485, 0.0435626434150131, 0.00919667587971063,
0.241220354905637, 0.0188148171033879, 0.0116570772346002, 0.0159496690575734,
0.00518918742249186, 0.0319701660388646, 0.100234998067917, 0.0119794232466471,
0.00123658099677804, 0.00178774726967923, 0.00215162606048125,
0.028398874195245, 0.02727277199735, 0.0536089031118459, 0.00567355556708304,
0.00182798929912398, 0.0221311523302337, 0.0317268552025847,
0.241167765332718, 0.201815176728704, 0.00750328900855035, 0.00346824263327472,
0.00859464311717095, 0.00488864781312837), X2 = c(0.0123677690429329,
0.0275541038901166, 0.0166991553536275, 0.0260168210079643, 0.00693728726147325,
0.00464096927279578, 0.0124618831179862, 0.0184586073538044,
0.00569866130459529, 0.00293809224808261, 0.00119326039429657,
0.00316749683866091, 0.00419982136508501, 0.00140900547876921,
0.00110999833888004, 0.00276678547598108, 0.0162868658191231,
0.0649037872628959, 0.00123222675644274, 0.00171687152904065,
0.0152583510689248, 0.0258721612337077, 0.0392641646035583, 0.361960538193137,
0.0357326269142103, 0.0107540980920499, 0.22279499286353, 0.0301267823507665,
0.0144535141006957, 0.0124677305707919, 0.00520476987173168,
0.0320510777198151, 0.0770024283430764, 0.00793648556749427,
0.000401508352378066, 0.000498605187176815, 0.000982487695277534,
0.0399009464278308, 0.0698023981838097, 0.0506533144316593, 0.00462517180839983,
0.00275731807224233, 0.0374332227392187, 0.0582978817333271,
0.121896031487931, 0.236774303454737, 0.0106755443754257, 0.00398238213200619,
0.0113833654830731, 0.00708983623072867), X3 = c(0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0)), row.names = c(NA, 50L), class = "data.frame")
and the code I use:
par(mfrow = c(1,2),
oma=c(5,5,0,0) + 0.0,
mar=c(0,0,1,1) + 0.0)
val.prob.ci.2(p = lo$X1, y = lo$X3, smooth="loess", legendloc=F, lwd.smooth=2, lwd.ideal=2, lty.ideal=2, dostats = F, axes = F)
axis(side=2,at=c(0,0.2,0.4,0.6,0.8,1))
box(which="plot")
text(x=0, y=1, adj=0, "LG", cex=1.3)
val.prob.ci.2(p = lo$X2, y = lo$X3, smooth="loess", legendloc=F, lwd.smooth=2, lwd.ideal=2, lty.ideal=2, dostats = F, axes = F)
box(which="plot")
text(x=0, y=1, adj=0, "rf", cex=1.3)
title(xlab = list("Estimated probability",cex=1.5),
ylab = list("Observed proportion",cex=1.5),
outer = TRUE)#, line=3)
the output is:
The problem lies with the second plot. Why it does not appear?

How to create and export multiple plots to jpeg format in r?

I have been creating a bar plot for the result of a sentiment analysis model in R. The data is very confidential feedbacks from the customers. So, the feedbacks are then fed into a sentiment analysis model to generate outputs. My work is to generate a chart for each combination for example zone = delhi and delhi has sub zones like eastdelhi, westdelhi,northdelhi,southdelhi. I want to generate charts with combination like
zone = delhi and sub-zone = eastdelhi. And I want to save it to a jpeg file.I have written a for loop to do so. But for some reason it isn't working. This is the code
#Set locales
rm(list = ls())
Sys.setlocale(category = "LC_ALL",locale = "English")
#Load libraries
LoadLibraries <- c("openxlsx",
"dplyr",
"tidyr",
"plotly",
"RColorBrewer",
"shiny",
"officer",
"parallel",
"dplyr",
"tidyr",
"magrittr",
"knitr")
lapply(LoadLibraries, require, character.only = TRUE)
path = "C:/Users/R_Visual/Data/visual_data.xlsx"
input_data <- read.xlsx(path)
name <- names(input_data[,1:10])
#Filtering the zones and circles
for (i in 1:length(unique(Zone.Final))){
for (j in 1:length(unique(Circle.Final))){
fileName = 'C:/Users/R_Visual/'+ str(i) + str(j) + '.jpeg'
jpeg(fileName, width = 900, height = 450)
df <- input_data %>%
filter(input_data$Zone.Final[i])
df <- df %>%
filter(df$Circle.Final[j])
color <- c("#ca2f27","#f56d43","#f8c38a","#fde08b","#d9ef8b","#a7d86f","#67bd64","#1a984f","#D3D3D3","#A9A9A9")
plot <- barplot(sort(colSums(input_data[, 1:10])),
main = paste("Sentiment Analysis for Zone",df$Zone.Final[i]," and Circle",df$Circle.Final[j], sep = ""),
xlab = "Sentiments",
ylab = "Count",
horiz = FALSE,
names = name,
col = color,
border = FALSE,
legend = TRUE,
beside = TRUE,
legend.text = name,
args.legend = list(bty = "n", x = "topleft",ncol = 1, cex = 0.8, y.intersp = 0.8, x.intersp = 0.25, horiz = F, xpd = TRUE, inset = c(0,0)))
dev.off()
}
}
EDIT:
This is the sample of input_data
> dput(input_data)
structure(list(anger = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), anticipation = c(1,
0, 0, 0, 0, 0, 1, 0, 0, 0), disgust = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), fear = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), joy = c(0,
0, 0, 0, 0, 0, 1, 0, 0, 0), sadness = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), surprise = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), trust = c(0,
0, 1, 1, 1, 0, 2, 0, 0, 0), negative = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), positive = c(1, 0, 0, 0, 1, 1, 2, 1, 0, 1), Zone.Final = c("Delhi",
"Lucknow", "Durgapur", "Lucknow", "Mumbai", "Bhopal", "Chandigarh",
"Chandigarh", "Gurugram", "Chandigarh"), Circle.Final = c("Noida",
"Gorakhpur", "Murshidabad", "Gorakhpur", "Mumbai City", "Bhopal",
"Chandigarh", "Panchkula", "Hisar", "Karnal")), row.names = c(NA,
10L), class = "data.frame")
If anyone could help me with the code, it would be of great help.
You can try creating a list combining the zone and subzone:
#Data
input_data <- structure(list(anger = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), anticipation = c(1,
0, 0, 0, 0, 0, 1, 0, 0, 0), disgust = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), fear = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), joy = c(0,
0, 0, 0, 0, 0, 1, 0, 0, 0), sadness = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), surprise = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), trust = c(0,
0, 1, 1, 1, 0, 2, 0, 0, 0), negative = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), positive = c(1, 0, 0, 0, 1, 1, 2, 1, 0, 1), Zone.Final = c("Delhi",
"Lucknow", "Durgapur", "Lucknow", "Mumbai", "Bhopal", "Chandigarh",
"Chandigarh", "Gurugram", "Chandigarh"), Circle.Final = c("Noida",
"Gorakhpur", "Murshidabad", "Gorakhpur", "Mumbai City", "Bhopal",
"Chandigarh", "Panchkula", "Hisar", "Karnal")), row.names = c(NA,
10L), class = "data.frame")
#Code
#First create and global id to combine zone and subzone
df <- input_data
df$id <- paste(df$Zone.Final,df$Circle.Final,sep = '-')
#Split
List <- split(df,df$id)
#Plot
color <- c("#ca2f27","#f56d43","#f8c38a","#fde08b","#d9ef8b","#a7d86f","#67bd64","#1a984f","#D3D3D3","#A9A9A9")
#Plot names
vnames <- paste0(names(List),'.jpeg')
#Loop
for(i in 1:length(List))
{
name <- names(List[[i]][, 1:10])
#Plot
jpeg(filename = vnames[i], width = 900, height = 450)
barplot(sort(colSums(List[[i]][, 1:10])),
main = paste("Sentiment Analysis for Zone ",
unique(List[[i]]$Zone.Final),
" and Circle ",unique(List[[i]]$Circle.Final), sep = ""),
xlab = "Sentiments",
ylab = "Count",
horiz = FALSE,
names = name,
col = color,
border = FALSE,
legend = TRUE,
beside = TRUE,
legend.text = name,
args.legend = list(bty = "n", x = "topleft",ncol = 1,
cex = 0.8, y.intersp = 0.8, x.intersp = 0.25,
horiz = F, xpd = TRUE, inset = c(0,0)))
dev.off()
}
That will create the plots. Of course you can add a path to vnames like the dir you have to save the plots in that folder.

3D plot in R using persp3D - Axis issues

I have a matrix (m) and I'm trying to plot a 3D representation of that matrix.
> dput(head(m))
structure(c(21930, 21844, 21758, 21672, 21586, 21500, 22016,
21930, 21844, 21758, 21672, 21586, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(6L, 26L), .Dimnames = list(
NULL, c("freq.min", "freq.max", "X0", "X1", "X2", "X3", "X4",
"X5", "X6", "X7", "X8", "X9", "X10", "X11", "X12", "X13",
"X14", "X15", "X16", "X17", "X18", "X19", "X20", "X21", "X22",
"X23")))
I managed to plot a 3D surface plot, but both the axes and the axis labels are incorrect. Note that the 3D surface plot below uses the entire matrix rather than just the header, which I only include here as the dput for brevity.
persp3D(z = m[,3:26], col = "lightgrey", shade = 0.5, ticktype = "detailed", axes=T)
Let's start with the axes themselves: the axis that goes from X0 to X23 should be the X axis of the matrix (column names), but here it is regarded as the Y axis. The Y axis, regarded here as the X axis, ranges from 0 to 22016 in 256 intervals of 86.
I've spent the last many hours scouring the internet for answers on how to change the axis labels, but have not succeeded. From what I understand, if I turn off the axis argument in persp3D (axes=F) I can then customize the axis in a subsequent line, like so:
axis3d(edge= 'y+-', at =seq(0,23,by=1) ,
labels = seq(0,23,by=1))
However, a RGL device pops up, and only the axis is plotted without the actual plot itself, which stays unchanged in the built-in R graphics device.
How do I successfully change the axes?
Does this what you're looking for?
clab <- 0:23
rlab <- seq(0, 21586, 86)
cnum <- length(clab)
rnum <- length(rlab)
m <- matrix(
c(runif(0.5*cnum*rnum)-1, runif(0.5*cnum*rnum)+1),
rnum, cnum,
dimnames = list(rlab, clab))
library(rgl)
plot3d(
clab, rlab, t(m),
type="n",
aspect = c(100, 200, 20),
xlab = "x", ylab = "y", zlab = "z",
sub = "Grab me and rotate me!"
)
surface3d(
clab, rlab, t(m),
color = c("black", "white"),
alpha = 0.5,
add = TRUE
)
To change the axis you can interchange x and y and transpose z with t().
As a side note: I wrote two functions to transform 3D point cloud data from tall to wide format and vice versa: recexcavAAR::spatialwide and recexcavAAR::spatiallong. I find them quite useful to go back and forth between plotting and analysis. Maybe they're useful for you.
Edit:
Alternative solution with single call to persp3d
clab <- 0:23
rlab <- seq(0, 21586, 86)
cnum <- length(clab)
rnum <- length(rlab)
m <- matrix(
c(runif(0.5*cnum*rnum)-1, runif(0.5*cnum*rnum)+1),
rnum, cnum,
dimnames = list(rlab, clab))
library(rgl)
persp3d(
clab, rlab, t(m),
color = c("black", "white"),
alpha = 0.5,
aspect = c(100, 200, 20),
xlab = "x", ylab = "y", zlab = "z",
sub = "Grab me and rotate me!"
)

Plotting with ggplot2: "Error: Discrete value supplied to continuous scale" on categorical y-axis

The plotting code below gives Error: Discrete value supplied to continuous scale
What's wrong with this code? It works fine until I try to change the scale so the error is there... I tried to figure out solutions from similar problem but couldn't.
This is a head of my data:
> dput(head(df))
structure(list(`10` = c(0, 0, 0, 0, 0, 0), `33.95` = c(0, 0,
0, 0, 0, 0), `58.66` = c(0, 0, 0, 0, 0, 0), `84.42` = c(0, 0,
0, 0, 0, 0), `110.21` = c(0, 0, 0, 0, 0, 0), `134.16` = c(0,
0, 0, 0, 0, 0), `164.69` = c(0, 0, 0, 0, 0, 0), `199.1` = c(0,
0, 0, 0, 0, 0), `234.35` = c(0, 0, 0, 0, 0, 0), `257.19` = c(0,
0, 0, 0, 0, 0), `361.84` = c(0, 0, 0, 0, 0, 0), `432.74` = c(0,
0, 0, 0, 0, 0), `506.34` = c(1, 0, 0, 0, 0, 0), `581.46` = c(0,
0, 0, 0, 0, 0), `651.71` = c(0, 0, 0, 0, 0, 0), `732.59` = c(0,
0, 0, 0, 0, 1), `817.56` = c(0, 0, 0, 1, 0, 0), `896.24` = c(0,
0, 0, 0, 0, 0), `971.77` = c(0, 1, 1, 1, 0, 1), `1038.91` = c(0,
0, 0, 0, 0, 0), MW = c(3.9, 6.4, 7.4, 8.1, 9, 9.4)), .Names = c("10",
"33.95", "58.66", "84.42", "110.21", "134.16", "164.69", "199.1",
"234.35", "257.19", "361.84", "432.74", "506.34", "581.46", "651.71",
"732.59", "817.56", "896.24", "971.77", "1038.91", "MW"), row.names = c("Merc",
"Peug", "Fera", "Fiat", "Opel", "Volv"
), class = "data.frame")
The plotting code:
## Plotting
meltDF = melt(df, id.vars = 'MW')
ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = MW, y = variable)) +
scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200)) +
scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))
Here's how the plot looked before adding scale:
As mentioned in the comments, there cannot be a continuous scale on variable of the factor type. You could change the factor to numeric as follows, just after you define the meltDF variable.
meltDF$variable=as.numeric(levels(meltDF$variable))[meltDF$variable]
Then, execute the ggplot command
ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = MW, y = variable)) +
scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200)) +
scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))
And you will have your chart.
Hope this helps
if x is numeric, then add scale_x_continuous(); if x is character/factor, then add scale_x_discrete(). This might solve your problem.
In my case, you need to convert the column(you think this column is numeric, but actually not) to numeric
geom_segment(data=tmpp,
aes(x=start_pos,
y=lib.complexity,
xend=end_pos,
yend=lib.complexity)
)
# to
geom_segment(data=tmpp,
aes(x=as.numeric(start_pos),
y=as.numeric(lib.complexity),
xend=as.numeric(end_pos),
yend=as.numeric(lib.complexity))
)

Resources