Question
Asked 29th May, 2018

How to resolve "Error in plot.new() : figure margins too large" in R Studio?

Hello everyone,
Currently I am trying to do K - mean clustering on microarray dataset which consists of 127 columns and 1000 rows. When I plot the graph, it gives an error like "figure margins too large". Then, I write this in R console:
par("mar") #It will give current dimensions
par(mar=c(1,1,1,1) #Tried to update the dimensions
But; it did not work. So, can anyone suggest me another way of fixing this problem? (Attached the some part of code in below)
Thanks,
Hasan
--------------------------------------------------------------------------------------------------------------
x = as.data.frame(x)
km_out = kmeans(x, 2, nstart = 20)
km_out$cluster
plot(x, col=(km.out$cluster+1), main="K - Means Clustering Results with K=2",xlab"", ylab"", pch=20, cex=2)
>Error in plot.new() : figure margins too large

Most recent answer

7th Jul, 2021
Simone Anzà
Georg-August-Universität Göttingen
Usually with me it works, try to run:
windows() that will open a new plotting window and then run plot.new() command

Popular answers (1)

3rd Sep, 2019
Virendra Kumar
RAMANUJAN COLLEGE

All Answers (16)

29th May, 2018
Jochen Wilhelm
Justus-Liebig-Universität Gießen
If x is a data.frame, plot(x) will produce a pairs plot. If x has n columns, a matrix of n x n plots will be drawn. If n is too large, the individual ploty will bee too small (smaller than the space needed already for the margins).
Check how many columns there are in x and if you really want a pairs plot. You may consider a heatmap, using RowSideColors indicating the cluster membership.
1 Recommendation
30th May, 2018
Hasan Alanya
Thank you for your response, @DrJochen. I will try your suggestion and hope it will fix the problem.
30th May, 2018
Hasan Alanya
Hi @Valentin, thank you for your interest. I already tried to move the borders of RStudio but it did not fix the problem, but I will try your second recommendation.
31st May, 2018
Flavio Maria Emanuele Pons
CNRS - IPSL
I agree with @Valentin, try
pdf('myplot.pdf')
plot(x, col=(km.out$cluster+1), main="K - Means Clustering Results with K=2",xlab"", ylab"", pch=20, cex=2)
dev.off()
this will produce a pdf file (you can also do the same with .eps format) in the directory.
1 Recommendation
31st May, 2018
Hasan Alanya
Thanks @Flavio !
8th Jun, 2018
Mar Muñiz Moreno
University of Miami Miller School of Medicine
Hi Hasan, another suggestion, following @valentin and @Flavio. When you are plotting such big plots it may happen that just saving it as a pdf with default width and height parameters will produce a heatmap that has superpositions or some parts can be cut off. If you cannot modify the plot features sizes at least what you can do is customize the pdf size to be able to plot big plots and see all the features in them.
> pdf('name.pdf',width = 8.267, height = 11.692)
Best,
Mar
2 Recommendations
3rd Sep, 2019
Virendra Kumar
RAMANUJAN COLLEGE
9th Jul, 2020
Yiyi Dong
University of Florida
I think you could try use the "par(mar = rep(m, n))" to limit the plotting.
3 Recommendations
3rd Dec, 2020
Omar Rafique
Harvard University
Increase the size of plot window by dragging the borders.
17th Jan, 2021
Abdul Haq
Quaid-i-Azam University
Increase the size of plot window by dragging the borders and then try to execute the command, hope it works for you. Good luck Hassan
22nd Feb, 2021
Ayaovi Agbessenou
icipe – International Centre of Insect Physiology and Ecology
RStudio causes an error when the plot panel is too small to display the chart. Simply expanding the plot panel fixes the bug and displays the chart.
25th Feb, 2021
Na Zhan
The University of Hong Kong
I would like to provide one solution in my special case: when I plotted a heat map of a network meta-analysis, I added "nchar.trts = 4", it helped to shorten the labels of different variables, and then it succeeded. Hope this would be helpful.
1 Recommendation
20th Mar, 2021
Rosh Pk
Karunya University
@Yiyi Dong added an answer
July 9, 2020
I think you could try use the "par(mar = rep(m, n))" to limit the plotting.
Sir this works thank you .
and Hasan Alanya sir you can try this it confines the plot area.
13th Apr, 2021
Himanshu Shekhar Palei
North Orissa University
First, remove plot history from plot tabs using broomstick, then try, if not work, then you can use "par(mar=rep(2, 4))".
7th Jul, 2021
Simone Anzà
Georg-August-Universität Göttingen
Usually with me it works, try to run:
windows() that will open a new plotting window and then run plot.new() command

Similar questions and discussions

R programming - error : non-numeric argument to binary operator ?
Question
4 answers
  • Molly McCannonMolly McCannon
I am running this code (below) to add error bars onto my bar chart, i am trying to to work out the standard deviation (sd) of shannons diversity index
The code works up until the second/third last line
geom_errorbar(aes(ymin=shannon-sd, ymax=shannon+sd), width=0.2,
position=position_dodge(0.9))
This error code keeps coming up
"Error in shannon - sd : non-numeric argument to binary operator"
but this is my data...
Exposure genus shannon sd
Exposed Crustacean 0.000000 0.00000000
Exposed Mollusc 1.199625 0.13291129
Exposed Seaweed 1.513125 0.42093822
Sheltered Crustacean 0.025500 0.07212489
Sheltered Mollusc 1.156750 0.26763341
Sheltered Seaweed 1.848125 0.27264128
anyone know where im going wrong?
library(ggplot2)
#+++++++++++++++++++++++++
# Function to calculate the mean and the standard deviation
# for each group
#+++++++++++++++++++++++++
# data : a data frame
# varname : the name of a column containing the variable
#to be summariezed
# groupnames : vector of column names to be used as
# grouping variables
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
df3 <- data_summary(diversity, varname="shannon",
groupnames=c("Exposure", "genus"))
# Convert dose to a factor variable
df3$genus=as.factor(df3$genus)
head(df3)
p <- ggplot(diversity, aes(x=genus, y=shannon, fill = location)) +
geom_bar(stat="identity", width = 0.5, position=position_dodge()) + theme_minimal() +
geom_errorbar(aes(ymin=shannon-sd, ymax=shannon+sd), width=0.2,
position=position_dodge(0.9))
p + scale_fill_brewer(palette="Paired") + theme_minimal()
Model selection by The Akaike’s Information Criterion (AIC) what is common practice?
Question
23 answers
  • Ronny SteenRonny Steen
When model fits are ranked according to their AIC values, the model with the lowest AIC value being considered the ‘best’. Models in which the difference in AIC relative to AICmin is < 2 can be considered also to have substantial support (Burnham, 2002; Burnham and Anderson, 1998).
To my knowledge it is common to seek the most parsimonious model by selecting the model with fewest predictor variables among the AIC ranked models. Hence, a variable qualifies to be included only if the model is improved by more than 2.0 (AIC relative to AICmin is > 2).
What do others prefer to do?
1. Present all models in which the difference in AIC relative to AICmin is < 2 (parameter estimates or graphically).
2. Only present the model with lowest AIC value.
3. Take into account the number of predictor variables and select the one with fewest predictor variables among the AIC ranked models.
4. Take into account the number of predictor variables and select the one with fewest predictor variables among the AIC ranked models using the following criteria that a variable qualifies to be included only if the model is improved by more than 2.0 (AIC relative to AICmin is > 2).
5. Looking at p-values of the predictors in the ranked models in addition to the AIC value (e.g. sometimes the predictors are non-significant in the top ranked model, while the predictors in a lower ranked model could be significant).
Regards,
Ronny

Related Publications

Book
La investigación científica, comprendida como un proceso de generación de nuevos conocimientos o la comprobación de conocimientos ya desarrollados, tiene una estrecha relación con la estadística aplicada (bioestadística en salud). El propósito es mostrar esta estrecha relación entre la bioestadística y la investigación científica. Asimismo, se pres...
Article
Overview This section describes a minimum setup requirement and step-by-step procedure to setup an environment for Affymetrix oligo microarray analysis. A sample experiment with input and output files is also described for basic steps in "microarray data analysis". You can modify the procedure to fit your own analysis; however, it may require addit...
Got a technical question?
Get high-quality answers from experts.